Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to load a local JS file dynamically?

For development purposes, I'd like to be able to easily load locally-stored scripts into the browser instead of having to copy-paste to the console.

Creating a new <script> element isn't working, it gives a Not allowed to load local resource: file://.... error (in Chrome).

Also, creating a userscript won't work--I'd have to re-install it every time I make an edit.

Is there an alternative way to easily load a local script via a bookmarklet/etc?

like image 791
Manishearth Avatar asked May 16 '12 03:05

Manishearth


People also ask

How do you load JS file dynamically in react?

We start by creating an empty <script></script> tag in the memory as script and then assign the necessary attributes to its src and the id to identify the script later. Finally, we append the script to our <body></body> tag to actually load this.

How do I load a JavaScript file?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.


2 Answers

run chrome as: chrome.exe --allow-file-access-from-files from CLI

like image 187
mostafa amiri Avatar answered Sep 27 '22 23:09

mostafa amiri


Sadly, Chrome doesn't allow you to load local files via AJAX; however, you can work around this limitation by launching the browser with the flag --disable-web-security (details vary per host operating system).

like image 44
maerics Avatar answered Sep 27 '22 23:09

maerics