简体   繁体   中英

Loading external javascript in a bookmarklet via a script loader

Here is my current setup:

  1. I have a script on our Sharepoint.
  2. Each user adds this in a bookmarklet to use it.
  3. If I make an update, they have to go and set up the bookmark all over again.

What I want to do:

  1. User adds script loader to bookmark toolbar
  2. They click it, and it loads the script from our Sharepoint.

This way, if I need to make any changes, they don't have to do anything and changes will be reflected automatically.

My bookmarklets/scripts depend on jQuery to make ajax quests and just for general ease of use.

I am currently using this: http://benalman.com/projects/run-jquery-code-bookmarklet/

Is there a framework that I can use for this kind of thing? I know Visual Event uses a loader, but since it was compressed with Closure, I can't really tell what it's doing. I understand that since things are loaded asynchronously in Javascript, I would have to wrap all my code inside of jquery being loaded, which is fine.. I just need a way to do it.

all you need to do is move your bookmarklet code to an external js file, and then inject that file using a bookmarklet. That way, the bookmarklet injects the latest logic, and you don't have to ever re-bookmark again. in that external script, you can paste the jQuery.js file's contents above your JS code to make sure it runs as expected.

modify the url to point to your script:

javascript:(function (){document.getElementsByTagName('head')[0].appendChild(document.createElement('script')).src='http://domain.com/scripts/external.js?'+Math.random();}());

if your intranet has decent caching setup, you can remove the "+Math.random()" part, but on an intranet, performance is rarely a problem for on-demand single-url asset loading, the the random url ensure everyone always gets the latest copy.

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