Javascript 如何在浏览器中本地执行 .js 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27241815/
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
How do I execute .js files locally in my browser?
提问by Program Beast
Hello i was wondering how i can type a javascript game on textmate with my mac and have just a regular .js file but than take the .js file and open it and have it run in chrome like if i have it say "Hello World!" than Hello World! would show up in chrome.
你好,我想知道如何用我的 mac 在 textmate 上输入一个 javascript 游戏,并且只有一个普通的 .js 文件,但不是获取 .js 文件并打开它并让它在 chrome 中运行,就像我让它说“Hello World! ” 比你好世界!会出现在 chrome 中。
An example of what I'm after is in this video: http://vimeo.com/105955605
我所追求的一个例子是这个视频:http: //vimeo.com/105955605
回答by thataustin
Around 1:51 in the video, notice how she puts a <script>tag in there? The way it works is like this:
在视频的 1 点 51 分左右,注意她是如何<script>在里面放标签的?它的工作方式是这样的:
Create an html file (that's just a text file with a .htmlending) somewhere on your computer. In the same folder that you put index.html, put a javascript file (that's just a textfile with a .jsending - let's call it game.js). Then, in your index.htmlfile, put some html that includes the script tag with game.js, like Mary did in the video. index.htmlshould look something like this:
.html在您的计算机上的某处创建一个 html 文件(这只是一个带有结尾的文本文件)。在您放置的同一个文件夹中index.html,放置一个 javascript 文件(这只是一个带.js结尾的文本文件- 我们称之为game.js)。然后,在您的index.html文件中,放入一些包含脚本标记的 html game.js,就像 Mary 在视频中所做的那样。 index.html应该是这样的:
<html>
<head>
<script src="game.js"></script>
</head>
</html>
Now, double click on that file in finder, and it should open it up in your browser. To open up the console to see the output of your javascript code, hit Command-alt-j (those three buttons at the same time).
现在,在 finder 中双击该文件,它应该会在您的浏览器中打开它。要打开控制台以查看 javascript 代码的输出,请点击 Command-alt-j(同时按下这三个按钮)。
Good luck on your journey, hope it's as fun for you as it has been for me so far :)
祝你旅途愉快,希望它对你和我一样有趣:)
回答by hypery2k
If you're using Google Chrome you can use the Chrome Dev Editor: https://github.com/dart-lang/chromedeveditor
如果您使用的是 Google Chrome,则可以使用 Chrome Dev Editor:https: //github.com/dart-lang/chromedeveditor

