javascript Chrome 没有加载我的 js 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32050498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chrome isn't loading my js file
提问by Matt Ellen
My page is very basic
我的页面非常基础
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Title</h1>
<canvas id="can"></canvas>
<video autoplay id="vid" style="cursor:pointer"></video><br>
<script type="application/javascript;version=1.7" src="8bit.js"></script>
</body>
</html>
It loads the JavaScript Firefox, but does not in Chrome.
它会加载 JavaScript Firefox,但不会在 Chrome 中加载。
If I copy and paste the contents of the JavaScript file into the console in Chrome, then it runs fine.
如果我将 JavaScript 文件的内容复制并粘贴到 Chrome 的控制台中,则它运行良好。
Why doesn't it load from the script tag in Chrome?
为什么它不从 Chrome 中的脚本标签加载?
回答by Praveen Kumar Purushothaman
Essence from the MDN about let
keyword:
来自 MDN 的关于let
关键字的精华:
The keyword
let
is supported in Chrome only for testing purposes only in the Console and not in the normal flow. Chrome doesn't supportlet
but Firefox does.
let
Chrome 中支持该关键字仅用于控制台中的测试目的,而不是在正常流程中。Chrome 不支持,let
但 Firefox支持。
Replace the following:
替换以下内容:
<script type="application/javascript;version=1.7" src="8bit.js"></script>
With:
和:
<script type="text/javascript" src="8bit.js"></script>
Chrome makes a strict decision on type
attribute. So please use that carefully. Only for Firefox, this is valid:
Chrome 对type
属性做出了严格的决定。所以请谨慎使用。仅适用于 Firefox,这是有效的:
Only available to code blocks in HTML wrapped in a
<script type="application/javascript;version=1.7">
block (or higher version). Beware, however, that as this is a non-standard feature, this will most likely break support for other browsers. XUL script tags have access to these features without needing this special block.
仅适用于包含在
<script type="application/javascript;version=1.7">
块(或更高版本)中的 HTML 中的代码块。但是请注意,由于这是一项非标准功能,因此很可能会破坏对其他浏览器的支持。XUL 脚本标签无需此特殊块即可访问这些功能。
Workaround for Chrome:
Chrome 的解决方法:
To enable these special keywords, add the following code on the top of the script block:
要启用这些特殊关键字,请在脚本块的顶部添加以下代码:
"use strict";
回答by Ori Drori
You use the doctype for HTML5, and the script default is javascript, so this will also work:
您使用 HTML5 的 doctype,脚本默认为 javascript,因此这也适用:
<script src="8bit.js"></script>