我习惯包括和使用JS这样:
<script type='text/javascript' src='/path/to/script.js'></script>
....
<script type='text/javascript'>
alert('Do some stuff here, using resources defined in script.js.');
</script>
Run Code Online (Sandbox Code Playgroud)
出于好奇,有没有人知道有一个带有src attrib的内联脚本的规则,如:
<script type='text/javascript' src='/path/to/script.js'>
alert('Do some stuff here, using resources defined in script.js.');
</script>
Run Code Online (Sandbox Code Playgroud)
我可以测试在各种浏览器中会发生什么,但想知道官方行为是什么.
我想知道如果一个脚本标签同时具有"src"和内联脚本.我试过下面的代码.
<script src="http://yui.yahooapis.com/2.8.1/build/yahoo/yahoo-min.js" type="text/javascript" charset="utf-8">
alert('hello');
</script>
<script type="text/javascript" charset="utf-8">
alert(YAHOO);
</script>
Run Code Online (Sandbox Code Playgroud)
它似乎从未在Firefox和Chrome中执行过"alert('hello')".我试图将src更改为指向不存在的URI."alert('hello')"也没有被执行.
那么,如果脚本标记中有src属性,则始终会忽略内联脚本?
以下是有效的JavaScript吗?该变量是否可用于外部调用的脚本?
<script src="//blah">
var something = "";
</script>
Run Code Online (Sandbox Code Playgroud)
背景:我已经看到这用于自动生成的分析代码(不是谷歌),它真的很烦我,所以想知道我是否可以解决这个问题,或者这个变量是否会以某种方式被外部引用的脚本使用.