Closed Thread Icon

Preserved Topic: Calling different scripts (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=18525" title="Pages that link to Preserved Topic: Calling different scripts (Page 1 of 1)" rel="nofollow" >Preserved Topic: Calling different scripts <span class="small">(Page 1 of 1)</span>\

 
CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 02-21-2002 16:24

How can I call a different script depending on what browser the page is viewed in. I have a script that works as a browser sniffer and then depending on what browser is being used it calls a different script.

So I have in the browser sniffer, it detecting the browser. Then I have a variable that lists all the browsers that can display a JavaApplet. If it can display the JavaApplet then it writes the code called from a one JavaScript. If it CAN'T display the JavaApplet it calls a different JavaScript that writes code to do something else.

Here is a sample of what I have:

code:
//Do some browser sniffing and determine if it can display the JavaApplet
//then call the appropriate JavaScript
//Good browser
var is_good = (is_ie5, is_ie5up,is_nn6, is_nn6up);

if (is_good){
document.write("<script src=\"applet.js\"></script>")
}else{
document.write("("<script src=\"flipBook.js\"></script>");
}



In the applet.js it just has a bunch of document.write lines that has the JavaApplet code and then the flipBook.js has some functions to make pictures act as a flipBook.

For some reason I can't figure out what is wrong. They each work seperately but when I try it this way it doesn't work. Am I calling the scripts wrong or does this not work?

Thanks in advance.

Later,
C:\


~Binary is best~

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 02-21-2002 17:03

Say you had this:

<script>
document.write("blah blah blah
</script>

That would obviously be an error, right? Now look at this:

<script>
document.write("blah blah blah</script>");
</script>

The browser recognizes that as the same thing as the first error. Your script is terminated at the first </script> that the browser sees.

So change it to

document.write("blah blah blah<\/script>");

The "\" character escapes the following character, and simultaneously removes the occurance of "</script>".

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 02-21-2002 17:24

Doh!!!! I got one of the escapes in there but I forgot about the / on the </script> tag!

Thanks!

Later,
C:\


~Binary is best~

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 02-21-2002 17:52

Oh, that's cool. I had always done it this way:

document.write( "</scr")
document.write( "ipt>")

I like <\/script> better


Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 02-21-2002 17:53

Well, understand that the "/" character is completely fine in strings without being escaped. It's just that when the browser sees </script> it thinks your script has ended, and that you forgot to end your string.

Doing

document.write("("<script src=\"flipBook.js\"><" + "/script>");

should work fine also.

[edit] Another note. I believe that the W3C standards say that, in XHTML at least, the script should end not only at the first "</script>", but it should in fact end at the first occurance of "</". So use "<\/" any time you would otherwise use just "</".

Then again, if you're using XHTML, you should realize that characters such as an ampersand need to be escaped like &amp;amp; , even in script and style tags, and since lots of old browsers won't understand that, it's best to put the script in an entirely separate file anyway.

[This message has been edited by Slime (edited 02-21-2002).]

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 02-21-2002 20:18

I just thought of something.

If I just want to make sure that the browser can display a Java Applet, would it be better to test the browser if it has Java enabled or just do it with the browser version and stuff?

Later,
C:\


~Binary is best~

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 02-21-2002 21:27

If you can test, test. There are really a lot more browser versions than we pretend, if you count minor versions and bug fixes, and then there are browsers that you and I don't even know about, etc.

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 02-21-2002 21:37

I have a pretty extensive list of browsers for detection. The only problem is, is that they may a browser that can use Java but have it disabled.

I was testing my page in NN4.7 at home and the applet didn't come up but it did at work. Same case with Opera5. I thought that I had Java enabled on both at home but I have to check to make sure.

Could it be the version of Java that I have for NN and Opera at home? If so I don't know how to determine the version of Java only if it is enabled or not.

Thanks for any help.

Later,
C:\


~Binary is best~

« BackwardsOnwards »

Show Forum Drop Down Menu