直接从浏览器执行 JavaScript 文件
这听起来像是一个琐碎的问题,但我真的需要知道。
如果您将 HTML 文件的 URL 放入浏览器的位置栏中,它将呈现该 HTML。这就是浏览器的全部目的。
如果你给它一个 JPG、一个 SWF、甚至 PDF,它就会为这些数据类型做正确的事情。
但是,如果您为其提供 JavaScript 文件的 URL,它将显示该文件的文本。我想要的是直接执行该文件。
现在,我知道如果您使用 javascript:
协议,它将执行 URL 的文本,但这不是我需要的。
我可以让 URL 指向一个由单个 标记组成的 HTML 文件,该标记又指向 JavaScript 文件,但由于我自己的神秘原因,我不能这样做。
如果 http://example.com/file.js
处的文件完全由以下组成
alert("it ran");
并且我将该 URL 放在位置栏中,我希望“it ran”作为警报弹出。
我怀疑这是否可能,但我希望有一个标头或 MIME 类型或类似的东西,我可以设置并奇迹般地实现这一点。
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.
If you give it a JPG, or a SWF, or even PDF, it will do the right things for those datatypes.
But, if you give it the URL of a JavaScript file, it will display the text of that file. What I want is for that file to be executed directly.
Now, I know that if you use the javascript:
protocol, it will execute the text of the URL, but that isn't what I need.
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.
If the file at http://example.com/file.js
consists entirely of
alert("it ran");
And I put that URL in the Location bar, I want "it ran" to pop up as an alert.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是不可能的。浏览器不知道 JavaScript 应该在什么上下文中运行;例如,
window
的属性是什么?如果您假设它可以提供一些随机默认值,那么document
的行为又如何呢?如果有人执行document.body.innerHTML = "foo"
会发生什么?JavaScript 与图像或 HTML 页面不同,它依赖于它运行的上下文。该上下文可以是 HTML 页面,也可以是 Node 服务器环境,甚至可以是 Windows 脚本主机。但是,如果您只是导航到 URL,浏览器不知道应该在什么上下文中运行脚本。
作为解决方法,可以使用
about:blank
作为主机页面。然后,您可以将脚本插入到文档中,通过将以下内容粘贴到 URL 栏中,为其提供适当的执行上下文: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 ofdocument
? If someone doesdocument.body.innerHTML = "foo"
what should happen?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.
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:或者你可以使用 RunJS: https://github.com/Dharmoslap/RunJS
然后你就可以只需拖放即可运行 .js 文件。
Or you can use RunJS: https://github.com/Dharmoslap/RunJS
Then you will be able to run .js files just with drag&drop.
不直接,但您可以制作一个简单的服务器端脚本,例如用 PHP。当然
,
您可以通过在 Apache 中使用
RewriteRule
和/或在主机文件中添加另一个重定向到127.0.0.1
的条目来缩小此范围。请注意,这在安全性方面并不是很好,但如果您自己使用它并且知道您正在下载什么,那么应该没问题。
Not directly, but you could make a simple server-side script, e.g. in PHP. Instead of
, navigate to:
Of course, you could smallen this by using
RewriteRule
in Apache, and/or adding another entry in your hosts file that redirects to127.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.
在地址栏中,您只需编写
javascript:/这里有一些 javascript 代码/;void(0);
http://www.javascriptkata。 com/2007/05/01/execute-javascript-code-directly-in-your-browser/
In the address bar, you simply write
javascript:/some javascript code here/;void(0);
http://www.javascriptkata.com/2007/05/01/execute-javascript-code-directly-in-your-browser/
使用 Node.js。
下载并安装node.js并创建一个http/s服务器并写下您想要在浏览器中显示的内容。
使用服务器上的 localhost::portNumber 作为运行文件的 url。
请参阅 Node js 文档 - https://nodejs.org/ dist/latest-v7.x/docs/api/http.html
运行 - http://localhost:3000
样本代码如下:
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
Run - http://localhost:3000
sample code below :
您可以使用 qt /webkit 编写自己的浏览器并执行此操作。
当用户在 url 位置输入 js 文件时,您可以读取该文件并执行 javascript 。
http://code.google.com/apis/v8/get_started.html是另一个频道。
不确定它是否满足您的需要。
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 .
http://code.google.com/apis/v8/get_started.html is another channel.
not sure if it meets ur need.