Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

calling javascript w/in javascript



12:20 am on Mar 14, 2006 (gmt 0)

5+ Year Member

I need to call different external javascript pages based on the user's screen resolution. I tried this:

<script language="JavaScript" type="text/javascript">
if (screen.width<1024) { document.write("<script language='JavaScript' src='/small-script.js'></script>"); }
else { document.write("<script language='JavaScript' src='/large-script.js'></script>"); }

This works in IE & Opera, but Netscape reads the </script> in the document.write sections and ends the javascript, then literally writes everything after it to the page. I've tried various solutions, including:

1) switching single & double quotes
2) leaving out </script> in document.write
3) using two separate if statements instead of if/else
4) replacing if/else with:

var code = (screen.width<1024)?
"<script language='JavaScript' src='/small-script.js'></script>":
"<script language='JavaScript' src='/large-script.js'></script>"

Nothing seems to work. Any suggestions?


1:13 am on Mar 14, 2006 (gmt 0)

10+ Year Member

<\/script> :)


1:26 am on Mar 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

You have to break <script></script> up so the browser doesn't confuse it with html.

<script language="JavaScript" type="text/javascript">
if (screen.width<1024) { document.write("<scr"+"ipt language='JavaScript' src='/small-script.js'>"+"</scr"+"ipt>"); }
else { document.write("<scr"+"ipt language='JavaScript' src='/large-script.js'>"+"</scr"+"ipt>"); }


2:05 am on Mar 14, 2006 (gmt 0)

5+ Year Member

thanks, both.

Featured Threads

Hot Threads This Week

Hot Threads This Month