7

次の JavaScript は有効ですか? 変数は、外部から呼び出されたスクリプトで使用できますか?

<script src="//blah">

var something = "";

</script>

背景: 自動生成された分析コード (Google ではない) でこれが使用されているのを見たことがありますが、これを修正できるかどうか、またはこの変数が外部参照スクリプトによって何らかの形で使用されるかどうかを知りたいと思っています。

4

4 に答える 4

9

Read this, http://www.w3.org/TR/html4/interact/scripts.html#h-18.2.1

The script may be defined within the contents of the SCRIPT element or in an external file. If the src attribute is not set, user agents must interpret the contents of the element as the script. If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI. Note that the charset attribute refers to the character encoding of the script designated by the src attribute; it does not concern the content of the SCRIPT element.

于 2013-10-22T05:10:38.527 に答える
3

いいえ、そうではありません。ソースを参照すると、script タグ間のコードは無視されます。次のようにできます。

<script src="bla.js"></script>
<script>
var something = "";
</script>
于 2013-10-22T05:07:16.230 に答える
3

It only executes if it is explicitly processed. See: http://ejohn.org/blog/degrading-script-tags. Specifically:

We can verify these two facts with some tests:

Test 1: Verify that internal scripts aren’t executed, even if an external src is loaded.

Test 2: Verify that internal scripts aren’t executed, even if an external src is not loaded.

Together these indicate that the internal script is never loaded unless a script on the page takes action to eval the inline code.

于 2013-10-22T05:10:36.140 に答える