Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do you quote the string "</script>"

    Hi,

    As the subject says, I'm having trouble quoting the text </script>

    I have a method that uses single quote for quoting strings, e.g.

    myMethod('some string param'); // I can't pass the string '<script>var a=0;</script>'

    It throws an error unterminated string

    any idea why?

    TIA

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Use DOM methods to add script elements, not document.write:
    Code:
    var newScript = document.createElement('script');
    newScript.type = 'text/javascript';
    newScript.src = 'myScript.js';
    document.getElementsByTagName('head')[0].appendChild(newScript);
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,628
    Thanks
    0
    Thanked 648 Times in 638 Posts
    The simplest change is to escape it like this to stop the </script> being interpreted as end-of-Javascript:

    '<script>var a=0;<\/script>'
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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