SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    script src inside script src

    hello and thanks for reading my post..

    this is the script
    HTML Code:
    <script language="JavaScript">
    document.write '<script language="JavaScript" src="http://site.com/page?ref=' + document.referrer + '"></script>';
    </script>
    all i get is this:
    ';

    why won't this work?
    what is the correct script?

    thanks!

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because the interpreter thinks that the script tag ends at the first </script>.
    Try this instead:
    Code:
    <script>
    <![CDATA[
    document.write '<script src="http://site.com/page?ref=' + document.referrer + '"></script>';
    ]]>
    </script>

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by camelsmoke View Post
    hello and thanks for reading my post..

    this is the script
    HTML Code:
    <script language="JavaScript">
    document.write '<script language="JavaScript" src="http://site.com/page?ref=' + document.referrer + '"></script>';
    </script>
    all i get is this:
    ';

    why won't this work?
    what is the correct script?

    thanks!
    One trouble is that the parameters for document.write needs to be in parenthesis.

    The main trouble though is that the browser gets to it first, and determines for itself what parts are script and what parts aren't.

    The </script> inside comments is causing troubles. Break up the word and you'll be fine.

    Code JavaScript:
    document.write('<script language="JavaScript" src="http://site.com/page?ref=' + document.referrer + '"></scr' + 'ipt>');

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    Because the interpreter thinks that the script tag ends at the first </script>.
    Try this instead:
    Code:
    <script>
    <![CDATA[
    document.write '<script src="http://site.com/page?ref=' + document.referrer + '"></script>';
    ]]>
    </script>
    When you do this though you've gotta be careful about older browsers, so you've gotta comment out the CDATA stuff so that javascript doesn't end up choking on it.

    HTML Code:
    <script>
    /* <![CDATA[ */
    document.write '<script src="http://site.com/page?ref=' + document.referrer + '"></script>';
    /* ]]> */
    </script>
    This partly why I try to stay away from the whole CDATA thing cause I don't want to add that to all my code, and instead deal with the actual issue itself by splitting the trouble text, or even better having the script itself in an external file.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,872
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    See http://javascript.about.com/library/bladdjs.htm for a way to do it without using document.write.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •