homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

calling javascript w/in javascript

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

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)

<\/script> :)


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

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)

thanks, both.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved