Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 995
Michael Lorton Avatar asked Oct 10 '11 19:10

Michael Lorton


People also ask

How a JavaScript program is executed by the browser?

The source code is passed through a program called a compiler, which translates it into bytecode that the machine understands and can execute. In contrast, JavaScript has no compilation step. Instead, an interpreter in the browser reads over the JavaScript code, interprets each line, and runs it.

Can you run a js file directly?

You can Run your JavaScript File from your Terminal only if you have installed NodeJs runtime. If you have Installed it then Simply open the terminal and type “node FileName. js”. If you don't have NodeJs runtime environment then go to NodeJs Runtime Environment Download and Download it.

How do I run a JavaScript file locally?

The easiest way to do this on Windows is to hold down the SHIFT key and then right click, then choose Open command window here. From the command window, type the command node and your command prompt will be transformed in to an interactive JavaScript REPL.


4 Answers

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); })();
like image 60
Domenic Avatar answered Oct 07 '22 21:10

Domenic


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

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

like image 37
Ladislav M Avatar answered Oct 07 '22 22:10

Ladislav M


Not directly, but you could make a simple server-side script, e.g. in PHP. Instead of

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.

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>
like image 26
pimvdb Avatar answered Oct 07 '22 22:10

pimvdb


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/

like image 38
PhilippeThomas Avatar answered Oct 07 '22 23:10

PhilippeThomas