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); })();
Answer from Domenic on Stack OverflowExecuting a JavaScript file directly from the browser - Stack Overflow
javascript - How do I execute .js files locally in my browser? - Stack Overflow
javascript - How To Run a Node JS file in a Live Browser - Stack Overflow
How to run Javascript program in browser instead of console when using Visual Studio 2019 - Stack Overflow
Videos
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.
Around 1:51 in the video, notice how she puts a <script> tag in there? The way it works is like this:
Create an html file (that's just a text file with a .html ending) somewhere on your computer. In the same folder that you put index.html, put a javascript file (that's just a textfile with a .js ending - let's call it game.js). Then, in your index.html file, put some html that includes the script tag with game.js, like Mary did in the video. index.html should look something like this:
<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).
Good luck on your journey, hope it's as fun for you as it has been for me so far :)
If you're using Google Chrome you can use the Chrome Dev Editor: https://github.com/dart-lang/chromedeveditor
You don't need an extension for that. Use the console.
To open the Console tab, do one of the following:
Use the keyboard shortcut Command-Option-J (Mac) or Control-Shift-J (Windows/Linux).
Select View > Developer > JavaScript Console.
Now you can execute any Javascript you want. Variables and functions that are defined in the global scope of the current document can also be used.
For example, if the current site has jQuery included, you can send JSON to a remote script:
$.post( "http://example.com/test.php", { foo: "bar" } );
Accepted answer works, but here is another method if you dont have access to the javascript console.
The javascript protocol allows you to run javascript on any website just by entering it into the url bar in this format: javascript:alert('Hello World');