detect if appended js is actually js

D

Dave

Folks,

There may be a trivial solution to this, but I'm not sure where to
begin...

I am have been writing a relatively simple js using JSON with a
callback. The problem is, the server from which I am pulling the data
may not be all that reliable. I have a setTimeout to accommodate a
404, but on occasion, this server might in fact produce something
other than js. Consequently, the appended function throws a syntax
error. The setTimeout I have will continue to work such that the user
will get some sort of visual cue. But, the syntax errors that may be
produce in the client's browser are annoying (i.e. HTML or SQL errors,
etc. may be returned and appended instead of the js I wanted). Is
there any way to detect if the data appended is in fact NOT js? The
only thing I can think of (but haven't done it yet) is to first append
the returned data into a hidden div, then use toJSONString(), & if the
data are good, append it back to document after first using parseJSON
on it. Eesh. There has to be a better way.

Thanks for any pointers,

Dave
 
B

BinnyVA

Hi,
Is there any way to detect if the data appended is in fact NOT js? The
only thing I can think of (but haven't done it yet) is to first append
the returned data into a hidden div, then use toJSONString(), & if the
data are good, append it back to document after first using parseJSON
on it. Eesh. There has to be a better way.
The simplest method I can think of is to check for the first
non-whitespace character. If it is a '<' there is no way it can be
json.

Another method is to eval it - try something like

var valid_json = true;
var json_string = "{'hi':5,'hello':[0,4]}";
try {
eval("(" + json_string + ")");
} catch(E) {
valid_json = false;
}

if(valid_json) alert("Its valid JSON");
else alert("JSON Invalid");

Two suggestions - nether very good. Your best option is to get a more
reliable server ;-)
 
D

Dave

Hi,> Is there any way to detect if the data appended is in fact NOT js? The
only thing I can think of (but haven't done it yet) is to first append
the returned data into a hidden div, then use toJSONString(), & if the
data are good, append it back to document after first using parseJSON
on it. Eesh. There has to be a better way.

The simplest method I can think of is to check for the first
non-whitespace character. If it is a '<' there is no way it can be
json.

Another method is to eval it - try something like

var valid_json = true;
var json_string = "{'hi':5,'hello':[0,4]}";
try {
eval("(" + json_string + ")");} catch(E) {

valid_json = false;

}

if(valid_json) alert("Its valid JSON");
else alert("JSON Invalid");

Two suggestions - nether very good. Your best option is to get a more
reliable server ;-)

Unfortunately, I'm stuck using this particular server because there is
no other in the world serving the data I need. So, let me get this
straight, there is a way to either check the first character or eval()
it when one is specifying a callback to obtain JSON from a server? How
do you capture the contents of the appended callback, that usually
contains a function of course, before it gets appended to the page? In
other words, how do you propose I obtain var json_string = "XX" when
in actual fact, "XX" is a function call.
 
D

Dave

Hi,> Is there any way to detect if the data appended is in fact NOT js? The
The simplest method I can think of is to check for the first
non-whitespace character. If it is a '<' there is no way it can be
json.
Another method is to eval it - try something like
var valid_json = true;
var json_string = "{'hi':5,'hello':[0,4]}";
try {
eval("(" + json_string + ")");} catch(E) {
valid_json = false;

if(valid_json) alert("Its valid JSON");
else alert("JSON Invalid");
Two suggestions - nether very good. Your best option is to get a more
reliable server ;-)

Unfortunately, I'm stuck using this particular server because there is
no other in the world serving the data I need. So, let me get this
straight, there is a way to either check the first character or eval()
it when one is specifying a callback to obtain JSON from a server? How
do you capture the contents of the appended callback, that usually
contains a function of course, before it gets appended to the page? In
other words, how do you propose I obtain var json_string = "XX" when
in actual fact, "XX" is a function call.- Hide quoted text -

- Show quoted text -

Hmm. Never mind. I'm hooked here because as near as I can tell,
there's no way to evaluate the content from the remote server if it's
anything other than js when one uses a callback because one has to
append content to the DOM before anything can be done with it. i.e.
all the eval() or the toJSONString() will only work once the content
is there in the DOM. If the appended content happens to be HTML, there
is obviously no callback function & so there is no way get at that
content to evaluate it...I think.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top