The code below works on a live site but I can\'t get it to run on the site jsfiddle .
See this for example.
Can anyone tell me why it\'s not working on jsfid
Look at:
https://stackoverflow.com/a/4935684/6796393
Or it is better to see this one:
https://stackoverflow.com/a/19110630/6796393
Here is what I tested:
function testJSON() {
var jsonString = '{"param1":"123","param2":"XXX78","param3":"11378"}';
var tmp_obj = eval('(' + jsonString + ')');
document.getElementById("result").innerHTML = tmp_obj.param2;
}
and
function testJSON() {
var jsonString = '{"param1":"123","param2":"XXX78","param3":"11378"}';
var tmp_obj = JSON.parse(jsonString);
document.getElementById("result").innerHTML = tmp_obj.param2;
}
It is better to use the second aproach.
P.S.> I came from How to parse parameters from JSON String using JS?
I found the same issue and solve it by changing Load Type
in the JavaScript settings:
No wrap-in<head>
or No wrap-in<body>
in the drop down JavaScript settings menu. Refer the below image.
JavaScript Settings Menu
The functions you define are defined in an onload function, so whereas before they were referenceable, because they are defined in that function they can only be referenced from within that function. You reference them as globals in your HTML. You have three options
a) ( easiest, quickest, not ideal ) - change function blah(){}
to window.blah = function(){};
making the functions global.
b) ( ideal way ) - use unobtrusive Javascript to attach behaviour to DOM elements from within the JS solely, meaning separate HTML from JS.
c) Make the jsfiddle not wrap the stuff onload. Change onLoad
to no wrap ( body or head ).
So instead of <p onclick="lol()" id="foo">
you'd do var e = document.getElementById('foo'); e.onclick = lol;
in the JS only.
I recommend b as it encourages best practices.
In your fiddle select no wrap (head)
in the dropdown on the left, click Run and it will work.
See example →
When onLoad
is selected your functions are defined within the closure of the $(document).ready(function() {});
only.