script标签的src属性

src属性指定外部文件路径,浏览器根据src属性值请求外部文件然后将外部文件的内容插入script标签之间,结果会覆盖原本script标签之间的内容,类似于document.write()会覆盖原本的dom一样。导致在编辑时写入的js代码不会被执行。
文件并非必须是js文件,任意类型的文本文件都可以,比如html,css,xml,txt。要求是这些文件里面的内容是js代码即可。
文件:"5.txt"
文件内容:console.log("5.txt");
<!-- 输出"5.txt" -->
    <script src="5.txt">
    console.log("hello");
 </script>

文件 :"6.php"

var a = 20;
<?php
    $b = 30;
    echo "console.log(a),$b"
?>

 

 

<!-- 输出 "20"  -->
<script src="script标签本质/6.php">
  console.log("hello");
</script>

 对于php文件,服务器只会解析<?php....?>之间的代码,除此之外的代码会被当做纯字符串原样返回给浏览器。

 

posted @ 2018-07-25 16:10  Tinypan  阅读(6627)  评论(0编辑  收藏  举报