How to include "<script>" in a string ?

Discussion in 'Javascript' started by Tom de Neef, Feb 24, 2008.

  1. Tom de Neef

    Tom de Neef Guest

    I have a second window for pop-ups.
    It is used to display text generated by the calling code, rather than a
    referenced html document.
    To execute links in that text, some script code needs to be added to the
    text. But I cannot get the script included.

    function PopUp(txt)
    {
    var head = '<head>'+
    '<script type="text/javascript"
    src="wwRegels.js"></script>'+
    '</head>'

    secondWindow.document.write('<html>'+head+'<body>'+txt+'</body></html>')
    secondWindow.document.close()
    if (window.focus) { secondWindow.focus() }
    }

    Browsers (IE, FF) object to the line starting with '<script (unterminated
    string literal) when loading the page. If I change "script" into "scrapt",
    loading completes without rrors. The browser seems to refuse "<script" as
    part of my string or I am blind to some mistake I've made. I have tried
    backslashes here and there but to no avail. Is there a way around ?

    TIA
    Tom
    Tom de Neef, Feb 24, 2008
    #1
    1. Advertising

  2. Tom de Neef wrote:
    > function PopUp(txt)
    > {
    > var head = '<head>'+
    > '<script type="text/javascript"
    > src="wwRegels.js"></script>'+
    > '</head>'
    >
    > secondWindow.document.write('<html>'+head+'<body>'+txt+'</body></html>')
    > secondWindow.document.close()
    > if (window.focus) { secondWindow.focus() }
    > }
    >
    > Browsers (IE, FF) object to the line starting with '<script (unterminated
    > string literal) when loading the page. If I change "script" into "scrapt",
    > loading completes without errors. The browser seems to refuse "<script" as
    > part of my string


    They should not and AFAIK they do not.

    > or I am blind to some mistake I've made.


    I see two probable possibilities for the observed behavior:

    a) You use the code as posted in which case the string literal is indeed
    not terminated (before the newline). Use string concatenation or a
    joined array to work around that.

    b) Because you have not escaped the ETAGO delimiter (`</') the script
    element that this code is contained in ends prematurely. Use
    `<\/', respectively.

    > I have tried backslashes here and there but to no avail.


    A useless statement.

    > Is there a way around ?


    Probably yes.


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
    Thomas 'PointedEars' Lahn, Feb 24, 2008
    #2
    1. Advertising

  3. Tom de Neef

    Tom de Neef Guest

    "Thomas 'PointedEars' Lahn" <> schreef in bericht
    news:...
    > Tom de Neef wrote:
    >> function PopUp(txt)
    >> {
    >> var head = '<head>'+
    >> '<script type="text/javascript"
    >> src="wwRegels.js"></script>'+
    >> '</head>'
    >>
    >> secondWindow.document.write('<html>'+head+'<body>'+txt+'</body></html>')
    >> secondWindow.document.close()
    >> if (window.focus) { secondWindow.focus() }
    >> }
    >>
    >> Browsers (IE, FF) object to the line starting with '<script (unterminated
    >> string literal) when loading the page. If I change "script" into
    >> "scrapt",
    >> loading completes without errors. The browser seems to refuse "<script"
    >> as
    >> part of my string

    >
    > They should not and AFAIK they do not.
    >
    >> or I am blind to some mistake I've made.

    >
    > I see two probable possibilities for the observed behavior:
    >
    > a) You use the code as posted in which case the string literal is indeed
    > not terminated (before the newline). Use string concatenation or a
    > joined array to work around that.
    >
    > b) Because you have not escaped the ETAGO delimiter (`</') the script
    > element that this code is contained in ends prematurely. Use
    > `<\/', respectively.
    >


    Thank you.
    a) the new line in this post was not in the code that I mailed. Clearly that
    was not the cause.
    b) '</script>' -> '<\/script>' that solves it !

    Tom
    Tom de Neef, Feb 24, 2008
    #3
  4. Tom de Neef wrote:
    > "Thomas 'PointedEars' Lahn" <> [wrote:]
    >> I see two probable possibilities for the observed behavior:
    >>
    >> a) You use the code as posted in which case the string literal is indeed
    >> not terminated (before the newline). Use string concatenation or a
    >> joined array to work around that.
    >>
    >> b) Because you have not escaped the ETAGO delimiter (`</') the script
    >> element that this code is contained in ends prematurely. Use
    >> `<\/', respectively.
    >>

    >
    > Thank you.
    > a) the new line in this post was not in the code that I mailed.


    Despite your using an application that is capable of sending e-mails, too,
    you did not mail anything here.

    > Clearly that was not the cause.


    Which should make clear to you that code should be posted in a fashion that
    automatic line-break near 80 characters does not cause a change in its
    meaning. Using spaces instead of tabs helps to achieve that, too.

    > b) '</script>' -> '<\/script>' that solves it !


    I expected that, as it is not exactly a new "problem". Please search before
    you post next time. And trim your quotes.

    http://jibbering.com/faq/


    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
    Thomas 'PointedEars' Lahn, Feb 24, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danny Anderson
    Replies:
    5
    Views:
    481
    Victor Bazarov
    Aug 15, 2003
  2. Rolf Magnus
    Replies:
    2
    Views:
    587
    Karl Heinz Buchegger
    Nov 28, 2003
  3. Elie Nader
    Replies:
    1
    Views:
    625
  4. Andreas Bogenberger
    Replies:
    3
    Views:
    882
    Andreas Bogenberger
    Feb 22, 2008
  5. Replies:
    10
    Views:
    156
Loading...

Share This Page