Executing a JavaScript file directly from the browser

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.


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, 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:

javascript:(function () { var el = document.createElement("script"); el.src = "PUT_URL_HERE"; document.body.appendChild(el); })();

Or you can use RunJS: https://github.com/Dharmoslap/RunJS

Then you will be able to run .js files just with drag&drop.

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 to

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.


   <? echo file_get_contents($_GET['url']); ?>




In the address bar, you simply write

javascript:/some javascript code here/;void(0);


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 :

  var http = require("http");
  var server = http.createServer(function(req,res){
       res.end("hello user");
  }); server.listen(3000);`

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.

Need Your Help

Is XPath much more efficient as compared to DOM and SAX?

xml dom xpath sax

I need to parse an xml string and find values of specific text nodes, attribute values etc.

jstree 3.0.0 contextmenu right click not working. Showing TypeError: vakata_context.element.html is not a function

javascript contextmenu jstree

My code is rendering tree, parent, child everything fine but right click contextmenu is not showing. Firebug shows error "TypeError: vakata_context.element.html is not a function". If I remove