简体   繁体   中英

How can I grab JavaScript code from a .js file on local drive and run it in IE11?

I have some JavaScript code stored in a .js file, which is currently stored on a network drive that's shared among myself and some others.

Now, I'm trying to create a second JavaScript script that grabs the code from the .js file and runs it in my browser. My intention is to create a static bookmarklet out of that second JavaScript script and give the bookmarklet to everyone on my team. That way, I can update the code in the .js file and everyone's bookmarklet will always point to the newest version of the .js code.

But how can I do so? So far, this is what my bookmarklet looks like:

javascript: (function() {
       var script = document.createElement('script');
    script.setAttribute('src', '\\SharedDrive\Scripts\LatestScript.js');
    document.getElementsByTagName('head')[0].appendChild(script);
})();

However, my browser console gets this error:

Loading failed for the with source “ http://myWebSite/SharedDrive%20Scripts%20LatestScript.js ”.

My code seems to think I'm trying to grab a file from an external website, when I'm really trying to grab a file from my network drive. How should I change my code to get this to work?

Note that this is in IE11.

I'm not sure if IE will completely block any file accesses, but if it doesn't, you need to format the URL as a file URL.

file://///SharedDrive/Scripts/LatestScript.js

Yes, five leading slashes. file:// to start the URL, / to say "start at the root of the file system," and another two // to lead a UNC path.

Note that if you're trying to insert a file URL into a webpage that's not also in a file:// origin, then the browser will likely block access to that resource being downloaded since it's a violation of cross-origin rules. So you might be better off sharing a bookmarklet that contains all the source code in a data: URL instead, which of course will unfortunately not stay up to date at all times. Alternately, host your injected script on some web server so that it can be downloaded by the host webpage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM