开发者

Javascript launching with (or without) document.writeln?

开发者 https://www.devze.com 2023-03-05 05:47 出处:网络
I have been looking at some tracking issues and have a question pertaining to the launching of scripts. Can someone tell me if there is a difference between:

I have been looking at some tracking issues and have a question pertaining to the launching of scripts. Can someone tell me if there is a difference between:

<script type="text/javascript" src="http://www.example.com/universalPixel.html"></script>

and

<script language="JavaScript"> document.writeln('<scri' + 'pt type="text/javascript" src="http://www.example.com/universalPixel.html"></scri' + 'pt>');</script>

I have seen these two variations and unfortunately have not been able to discern if there is a significant difference dependent on the inclusion of the document.writeln method. Or perhaps another distinction that I am unaware of.

Thank yo开发者_运维百科u for your help.

Alex


document.writeln is probably used to dynamically include a script, using that strange string concatenation method because the browser processes </script> appearing anywhere as the end of the script tag, even inside of a JavaScript string. So the following would not work correctly for that reason:

<script type="text/javascript">
    // code

    document.writeln('<script type="text/javascript" src="..."></script>");
    // ^^ the </script> above ends the previous <script> tag prematurely.

    // more code
</script>

But there are better methods:

  1. Just include them normally, using <script src="..">
  2. Dynamically generate includes using server-side code (i.e., PHP) instead of JavaScript
  3. Use a JS library that provides dynamic library loading.


Since document.writeLn is discouraged in XHTML, you should attempt to use option 1. Many strict browsers will even ignore the second item if the appropriate doctype is used.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号