Is there a way, like an extension or application, in Chrome to create and run .js
files in Chrome?

- 771
- 2
- 16
- 37

- 3,965
- 4
- 27
- 35
10 Answers
if you don't want to explicitly create a js file but still want to test your javascript code, you can use snippets to run your JS code.
Follow the steps here:
- Open Dev Tools
- Go to Sources Tab
- Under Sources tab go to snippets, + New snippet
- Paste your JS code in the editor then run Command + Enter on a Mac, or Ctrl + Enter on Windows or Linux. You should see the output in console if you are using console.log or similar to test. You can edit the current web page that you have open or run scripts, load more javascript files. (Just note: this snippets are not stored on as a js file, unless you explicitly did, on your computer so if you remove chrome you will lose all your snippets);
- You also have a option to save as your snippet if you right click on your snippet.

- 36,492
- 15
- 194
- 265

- 11,467
- 6
- 31
- 40
-
4This helped me immensely with automating form submission on a third-party site (had over 1,000 entries than needed to be submitted). Thank you! This should be the top answer. – StephanieQ Nov 19 '18 at 22:29
-
this works flawlessly...just remove – Gaurav Feb 05 '19 at 09:02
-
2Is there a way to run a script whenever a website is opened? – Maram Sreerama Reddy Mar 10 '19 at 09:11
-
2@Sreeram You can always write a chrome plugin and run your javascript there. – РАВИ Mar 14 '19 at 07:04
-
Exactly what I was searching for! Thanks a ton :) – Sijan Shrestha Jun 22 '19 at 12:10
-
1@Wira, you can turn your script into a clickable bookmarklet, [like I show here](https://stackoverflow.com/a/62710098/4561887). You'll have to open the website and then _click on the bookmarklet_ to run it, though. – Gabriel Staples Nov 23 '21 at 00:21
Try this:
1. Install Node.js from https://nodejs.org/
2. Place your JavaScript code into a .js file (e.g. someCode.js)
3. Open a cmd shell (or Terminal on Mac) and use Node's Read-Eval-Print-Loop (REPL) to execute someCode.js like this:
> node someCode.js
Hope this helps!

- 179
- 1
- 3
You should write in file:
<script>
//write your JavaScript code here
</script>
save it with .html extension and open with browser.
For example:
// this is test.html
<script>
alert("Hello");
var a = 5;
function incr(arg){
arg++;
return arg;
}
alert(a);
</script>

- 601
- 1
- 9
- 20
You need an HTML page to load a JS file.

- 15,525
- 4
- 56
- 83
-
2I just want to write and test some javascript funtions, then what kind of editor I can use in Chrome to create and run those .js files? – chaohuang Mar 16 '12 at 04:48
-
There's the chrome developer console, but it's not a code editor. You'd use a real editor for that. – Marc B Mar 16 '12 at 04:50
-
CTRL+SHIFT+J will open chrome dev tools, click on Console tab and test away. by the way same is available in Firebug firefox plugin – dbrin Mar 16 '12 at 04:51
-
Ok, after I create the js. file in, say vim, then how can I run it in Chrome? I mean just the .js file, and the whole html file. – chaohuang Mar 16 '12 at 04:55
-
1
-
You don't necessarily need to have an HTML page. Open Chrome, press Ctrl+Shift+j
and it opens the JavaScript console where you can write and test your code.

- 1,317
- 2
- 17
- 32
How to create a Javascript Bookmark in Chrome:
You can use a Javascript bookmark: https://helloacm.com/how-to-write-chrome-bookmark-scripts-step-by-step-tutorial-with-a-steemit-example/. Just create a bookmark to look like this:
Ex:
Name:
Test javascript bookmark in Chrome
URL:
javascript:alert('Hello world!');
Just precede the URL with javascript:
, followed by your Javascript code. No space after the colon is required.
Here's how it looks as I'm typing it in:
Now save and then click on your newly-created Javascript bookmark, and you'll see this:
You can do multi-line scripts too. If you include any comments, however, be sure to use the C-style multi-line comments ONLY (/* comment */
), and NOT the C++-style single-line comments (// comment
), as they will interfere. Here's an example:
URL:
javascript:
/* This is my javascript demo */
function multiply(a, b)
{
return a * b;
}
var a = 1.4108;
var b = 3.7654;
var result = multiply(a, b);
alert('The result of ' + a + ' x ' + b + ' = ' + result.toFixed(4));
And here's what it looks like as you edit the bookmark, after copying and pasting the above multi-line script into the URL field for the bookmark:
And here's the output when you click on it:
References:
- https://superuser.com/questions/192437/case-sensitive-searches-in-google-chrome/582280#582280
- https://gist.github.com/borisdiakur/9f9d751b4c9cf5acafa2
- Google search for "chrome javascript() in bookmark"
- https://helloacm.com/how-to-write-chrome-bookmark-scripts-step-by-step-tutorial-with-a-steemit-example/
- https://helloacm.com/how-to-write-chrome-bookmark-scripts-step-by-step-tutorial-with-a-steemit-example/
- https://javascript.info/hello-world
- JavaScript equivalent to printf/String.Format

- 36,492
- 15
- 194
- 265
Usually one uses text editor to create source files (like JavaScript). I use VisualStudio which have intellisence supprt for JavaScript, but any other editor will do (vim or notepad on Windows are both fine).
To run JavaScript by itself you need something that can do that. I.e. on Windows you can directly run script from console using CScript script.js
command. There are other ways to run JavaScript on Windows and other OS.
Browsers (like Chrome) do not run JavaScript by itself, only as part of a page or extensions. It is unclear what one would expect of browser to do with JavaScript by itself.

- 98,904
- 14
- 127
- 179
-
Thanks. I will write an HTML file including the javascript functions, then load the HTML file in Chrome to run those functions. – chaohuang Mar 16 '12 at 05:12
-
You can also open your js file path in the chrome browser which will only display text.
However you can dynamically create the page by including:
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'myjs.js';
document.head.appendChild(script);
Now you can have access to the js variables and functions in the console.
Now when you explore the elements it should have included.
So i guess you dont need a html file.

- 91
- 1
- 5
The easiest way is to run js file is to install nodejs in Your system and then go to the directory like shown in the below link
click to show picture
first, write node keyword and then type the name of your file
so to run your js code in node write like i.e. node index
I hope you understand this

- 209
- 3
- 6
-
1[DO NOT post images of code, data, error messages, etc](https://meta.stackoverflow.com/a/285557) – a.ak Jul 06 '21 at 05:07
Open a basic text editor and type out your html. Save it as .html If you type in file:///C:/ into the address bar you can then navigate to your chosen file and run it. If you want to open a file that is on a server type in file:/// and instead of C:/ the first letter of the server followed by :/.