javascript variable that contains [removed]

后端 未结 3 529
余生分开走
余生分开走 2020-12-11 04:51

I need a little assistance. I have to create a javascript string that contains more javascript that is then written to a div tag in the parent window. The code is as follo

相关标签:
3条回答
  • 2020-12-11 05:28

    You have to split the string:

    jstr2 += '<' + '/script>\n';
    

    It's also better to comment out everything inside the script:

    <script type="text/javascript">
    <!--//
        // your code here
    //-->
    </script>
    

    Or in HTML:

    <script type="text/javascript">
    //<![CDATA[
        // your code here
    //]]>
    </script>
    

    Or in XHTML:

    • same as HTML but #PCDATA instead of CDATA.
    0 讨论(0)
  • 2020-12-11 05:38

    The SCRIPT tag is content agnostic, so the parser just keeps running through the content until it finds a /SCRIPT sequence. When it does, it passes the content it's found to the JS environment for evaluation. That gives you your unterminated literal error because the sent content ends where your /SCRIPT begins. (There is no terminating quote mark to be found for the JS parser).

    Escaping the slash with backslash

    jstr2 += "<\/script>";
    

    or some other work-around hack breaks the trigger point in the sequence here and solves this problem (but still leaves you with some very dubious code).

    0 讨论(0)
  • 2020-12-11 05:49

    Write it as:

    jstr2 += '<\/script>\n';
    
    0 讨论(0)
提交回复
热议问题