Why split the [removed] tag when writing it with [removed]()?

前端 未结 5 1122
挽巷
挽巷 2020-11-21 07:37

Why do some sites (or advertisers that give clients javascript code) employ a technique of splitting the tags

相关标签:
5条回答
  • 2020-11-21 07:54

    I think is for prevent the browser's HTML parser from interpreting the <script>, and mainly the </script> as the closing tag of the actual script, however I don't think that using document.write is a excellent idea for evaluating script blocks, why don't use the DOM...

    var newScript = document.createElement("script");
    ...
    
    0 讨论(0)
  • 2020-11-21 07:54

    The </script> inside the Javascript string litteral is interpreted by the HTML parser as a closing tag, causing unexpected behaviour (see example on JSFiddle).

    To avoid this, you can place your javascript between comments (this style of coding was common practice, back when Javascript was poorly supported among browsers). This would work (see example in JSFiddle):

    <script type="text/javascript">
        <!--
        if (jQuery === undefined) {
            document.write('<script type="text/javascript" src="http://z-ecx.images-amazon.com/images/G/01/javascripts/lib/jquery/jquery-1.2.6.pack._V265113567_.js"></script>');
        }
        // -->
    </script>
    

    ...but to be honest, using document.write is not something I would consider best practice. Why not manipulating the DOM directly?

    <script type="text/javascript">
        <!--
        if (jQuery === undefined) {
            var script = document.createElement('script');
            script.setAttribute('type', 'text/javascript');
            script.setAttribute('src', 'http://z-ecx.images-amazon.com/images/G/01/javascripts/lib/jquery/jquery-1.2.6.pack._V265113567_.js');
            document.body.appendChild(script);
        }
        // -->
    </script>
    
    0 讨论(0)
  • 2020-11-21 08:07

    </script> has to be broken up because otherwise it would end the enclosing <script></script> block too early. Really it should be split between the < and the /, because a script block is supposed (according to SGML) to be terminated by any end-tag open (ETAGO) sequence (i.e. </):

    Although the STYLE and SCRIPT elements use CDATA for their data model, for these elements, CDATA must be handled differently by user agents. Markup and entities must be treated as raw text and passed to the application as is. The first occurrence of the character sequence "</" (end-tag open delimiter) is treated as terminating the end of the element's content. In valid documents, this would be the end tag for the element.

    However in practice browsers only end parsing a CDATA script block on an actual </script> close-tag.

    In XHTML there is no such special handling for script blocks, so any < (or &) character inside them must be &escaped; like in any other element. However then browsers that are parsing XHTML as old-school HTML will get confused. There are workarounds involving CDATA blocks, but it's easiest simply to avoid using these characters unescaped. A better way of writing a script element from script that works on either type of parser would be:

    <script type="text/javascript">
        document.write('\x3Cscript type="text/javascript" src="foo.js">\x3C/script>');
    </script>
    
    0 讨论(0)
  • 2020-11-21 08:11

    Here's another variation I've used when wanting to generate a script tag inline (so it executes immediately) without needing any form of escapes:

    <script>
        var script = document.createElement('script');
        script.src = '/path/to/script.js';
        document.write(script.outerHTML);
    </script>
    

    (Note: contrary to most examples on the net, I'm not setting type="text/javascript" on neither the enclosing tag, nor the generated one: there is no browser not having that as the default, and so it is redundant, but will not hurt either, if you disagree).

    0 讨论(0)
  • 2020-11-21 08:14

    The solution Bobince posted works perfectly for me. I wanted to offer an alternative method as well for future visitors:

    if (typeof(jQuery) == 'undefined') {
        (function() {
            var sct = document.createElement('script');
            sct.src = ('https:' == document.location.protocol ? 'https' : 'http') +
              '://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js';
            sct.type = 'text/javascript';
            sct.async = 'true';
            var domel = document.getElementsByTagName('script')[0];
            domel.parentNode.insertBefore(sct, domel);
        })();
    }
    

    In this example, I've included a conditional load for jQuery to demonstrate use case. Hope that's useful for someone!

    0 讨论(0)
提交回复
热议问题