JSON (json2.js) variable undefined in Opera 8.5
I'm using Douglas Crockford's javascript parsing library to开发者_StackOverflow中文版 parse some AJAX request responses to a js script I'm running on an old embedded device whose development ended with Opera 8.5 support (Presto 1.0 rendering engine, Linear B).
I've tried importing the script via script tag "src" attribute, and inserting the script verbatim in between the script tags. However as soon as reference is made to the JSON object I get an exception that reads "Reference to undefined variable: JSON".
I've tested global variables in disjoint scripts and that seems to be working fine.
It's curious considering the first lines of json2.js read:
var JSON;
if(!JSON) {
JSON = {};
It works fine in Opera9, which uses the same javascript engine, so I am completely stumped.
I've also tested to see if the notation is valid (yes), the script just doesn't want to be included, instrumentation placed anywhere in the text just does not get called.
Any suggestions?
Edit:
I've just concatenated all of the scripts together into a single source between script tags and it still fails to load, including failing to load all javascript that follows it. It has to be some kind of parse error.
Edit2:
Download Opera 8.5 here
http://arc.opera.com/pub/opera/
To your first question comments were added
// delcare a global variable named JSON.Nothing will happen if JSON already declared.
var JSON;
// if JSON not initlized
if(!JSON) {
JSON = {}; // initlize JSON object
}
To your second question Some browser(chrome,firefox,i don't know about opera) support JSON natively, you can test and fix by the code below
if (!window.JSON) {
//load JSON libaray
head = document.getElementsByTagName("head")[0];
script = document.createElement("script");
script.src = "path/to/json2.js";
script.onload = script.onreadystatechange = function () {
if (!this.readyState || this.readyState == "loaded" || this.readyState == "complete") {
head.removeChild(script); //remove the script tag we dynamically created
alert(JSON);
}
}
head.append(script);
}
精彩评论