Results 1 to 5 of 5

Thread: using document.write to write a script tag

  1. #1
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,634
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default using document.write to write a script tag

    Hi,

    I need to check if jQuery is loaded on a page, and (if not) write a script tag to load it. I found this suggestion by John Resig:
    Code:
    <script>
    if(typeof jQuery === "undefined"){
    	document.write('<script src="local/jquery.js"></script>');
    }
    </script>
    makes sense. And, looking at the discussion, it seems that it works for others.

    However, when I run it, I get this as the page output:
    Code:
    ");
    and a javascript error
    Code:
    unterminated string literal
    it seems that the </script> tag - inside the literal string - is being treated as an actual closing script tag, instead of as a string. I've tried switching around double- and single-quotes, to no avail. Any suggestions?
    Last edited by traq; 11-16-2010 at 04:41 AM.

  2. #2
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    Try escaping the characters ala: \<\/\s\c\r\i\p\t\>. Not sure if it work work, but it's worth a shot. If that doesn't work you could use <![CDATA or even "</scr"+"ipt>"
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,634
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    hmm... this works
    Code:
    <script>
    if(typeof jQuery === "undefined"){
    	document.write("<script src='local/jquery.js'></"+"script>")
    }
    </script>
    but I think it's stupid. Shouldn't the original version work?

    Edit: Thought of that while you were typing. I'm trying the <![CDATA[ ]]> idea now

    Edit: ...and now I'm getting ") } ]]> ...
    Last edited by traq; 11-16-2010 at 04:10 AM.

  4. #4
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    Quote Originally Posted by traq View Post
    hmm... this works
    Code:
    <script>
    if(typeof jQuery === "undefined"){
    	document.write("<script src='local/jquery.js'></"+"script>")
    }
    </script>
    but I think it's stupid. Shouldn't the original version work?
    No, it shouldn't because the page you are on is rendering HTML, technically anything between <script> </script> is sent for javascript processing. HTML does not know, care, or give a damn if your in a string. Thats why CDATA was invented, and the famous //<!--...
    Edit:
    ...and now I'm getting ") } ]]> ...
    try replacing </script> with <\/script>. However NORTON hates that and messes this up. PLEASE SEE THIS THREAD
    Last edited by fileserverdirect; 11-16-2010 at 04:14 AM.
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  5. The Following User Says Thank You to fileserverdirect For This Useful Post:

    traq (11-16-2010)

  6. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,634
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I suppose that's it, then. good resource, thanks
    Edit: here's more:
    Code:
    <script type="text/javascript">
    <!--//--><![CDATA[//><!--
    
    //JavaScript goes here
    
    //--><!]]>
    </script>
    sheesh...
    Last edited by traq; 11-16-2010 at 04:34 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •