直接从浏览器执行 JavaScript 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7717382/
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
Executing a JavaScript file directly from the browser
提问by Malvolio
This sounds like a trivia question but I really need to know.
这听起来像是一个琐碎的问题,但我真的需要知道。
If you put the URL of an HTML file in the Location bar of your browser, it will render that HTML. That's the whole purpose of a browser.
如果您将 HTML 文件的 URL 放在浏览器的位置栏中,它将呈现该 HTML。这就是浏览器的全部目的。
If you give it a JPG, or a SWF, or even PDF, it will do the right things for those datatypes.
如果您给它一个 JPG、SWF 或什至 PDF,它将为这些数据类型做正确的事情。
But, if you give it the URL of a JavaScript file, it will displaythe text of that file. What I want is for that file to be executed directly.
但是,如果您给它一个 JavaScript 文件的 URL,它将显示该文件的文本。我想要的是直接执行该文件。
Now, I know that if you use the javascript:
protocol, it will execute the textof the URL, but that isn't what I need.
现在,我知道如果您使用该javascript:
协议,它将执行URL的文本,但这不是我所需要的。
I could have the URL point to an HTML file consisting of a single <script>
tag that in turn points to the JavaScript file, but for occult reasons of my own, I cannot do that.
我可以让 URL 指向一个由单个<script>
标签组成的 HTML 文件,该文件又指向 JavaScript 文件,但由于我自己的神秘原因,我不能这样做。
If the file at http://example.com/file.js
consists entirely of
如果文件 athttp://example.com/file.js
完全由
alert("it ran");
And I put that URL in the Location bar, I want "it ran" to pop up as an alert.
我将该 URL 放在位置栏中,我希望“它已运行”作为警报弹出。
I'm skeptical that this is possible but I'm hoping-against-hope that there is a header or a MIME type or something like that that I can set and miraculously make this happen.
我怀疑这是可能的,但我希望-反对-希望有一个标题或 MIME 类型或类似的东西,我可以设置并奇迹般地使这发生。
采纳答案by Domenic
This is not possible. The browser has no idea what context the JavaScript should run in; for example, what are the properties of window
? If you assume it can come up with some random defaults, what about the behavior of document
? If someone does document.body.innerHTML = "foo"
what should happen?
这不可能。浏览器不知道 JavaScript 应该在什么上下文中运行;例如, 的属性是window
什么?如果您假设它可以提出一些随机默认值,那么document
? 如果有人做document.body.innerHTML = "foo"
了应该发生什么?
JavaScript, unlike images or HTML pages, is dependent on a context in which it runs. That context could be a HTML page, or it could be a Node server environment, or it could even be Windows Scripting Host. But if you just navigate to a URL, the browser has no idea what context it should run the script in.
与图像或 HTML 页面不同,JavaScript 依赖于它运行的上下文。该上下文可以是 HTML 页面,也可以是 Node 服务器环境,甚至可以是 Windows Scripting Host。但是如果你只是导航到一个 URL,浏览器不知道它应该在什么上下文中运行脚本。
As a workaround, perhaps use about:blank
as a host page. Then you can insert the script into the document, giving it the appropriate execution context, by pasting the following in your URL bar:
作为一种解决方法,也许about:blank
用作主机页面。然后,您可以通过在 URL 栏中粘贴以下内容,将脚本插入到文档中,为其提供适当的执行上下文:
javascript:(function () { var el = document.createElement("script"); el.src = "PUT_URL_HERE"; document.body.appendChild(el); })();
回答by Ladislav M
Or you can use RunJS: https://github.com/Dharmoslap/RunJS
或者你可以使用 RunJS:https: //github.com/Dharmoslap/RunJS
Then you will be able to run .js files just with drag&drop.
然后你就可以通过拖放来运行 .js 文件。
回答by pimvdb
Not directly, but you could make a simple server-side script, e.g. in PHP. Instead of
不是直接的,但您可以制作一个简单的服务器端脚本,例如在 PHP 中。代替
http://example.com/file.js
, navigate to:
, 导航:
http://localhost/execute_script.php?url=http://example.com/file.js
Of course, you could smallen this by using RewriteRule
in Apache, and/or adding another entry in your hosts file that redirects to 127.0.0.1
.
当然,您可以通过RewriteRule
在 Apache 中使用和/或在您的主机文件中添加另一个重定向到127.0.0.1
.
Note that this is not great in terms of security, but if you use it yourself and know what you're downloading, you should be fine.
请注意,这在安全性方面并不是很好,但是如果您自己使用它并且知道您正在下载什么,那么您应该没问题。
<html>
<head>
<script>
<? echo file_get_contents($_GET['url']); ?>
</script>
</head>
<body>
</body>
</html>
回答by PhilippeThomas
In the address bar, you simply write
在地址栏中,您只需写
javascript:/some javascript code here/;void(0);
javascript:/这里有一些 javascript 代码/;void(0);
http://www.javascriptkata.com/2007/05/01/execute-javascript-code-directly-in-your-browser/
http://www.javascriptkata.com/2007/05/01/execute-javascript-code-directly-in-your-browser/
回答by Sowmya M
Use Node.js. Download and install node.js and create a http/s server and write down what you want to display in browser. use localhost::portNumber on server as url to run your file. refer to node js doc - https://nodejs.org/dist/latest-v7.x/docs/api/http.html
使用 Node.js。下载并安装 node.js 并创建一个 http/s 服务器并写下您想要在浏览器中显示的内容。在服务器上使用 localhost::portNumber 作为 url 来运行您的文件。参考节点 js 文档 - https://nodejs.org/dist/latest-v7.x/docs/api/http.html
Run - http://localhost:3000
sample code below :
下面的示例代码:
var http = require("http");
var server = http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/html'});
res.end("hello user");
}); server.listen(3000);`
回答by ashok
you can write your own browser using qt /webkit and do that. when user enters a js file in url location you can read that file and execute the javascript .
您可以使用 qt /webkit 编写自己的浏览器并执行此操作。当用户在 url 位置输入 js 文件时,您可以读取该文件并执行 javascript 。
http://code.google.com/apis/v8/get_started.htmlis another channel. not sure if it meets ur need.
http://code.google.com/apis/v8/get_started.html是另一个渠道。不确定它是否满足您的需要。