Extending browser's JavaScript lib?

K

KKramsch

Increasingly often I find myself using brief JavaScript scripts
as pseudo-URLs in the location bar (i.e. using the javascript:
pseudoprotocol), particularly as bookmarks. It would be great if
these mini-scripts had access to utility functions previously
defined by me in a general utilities module. Is there any way to
tell the browser to "extend" its repertoire of JavaScript functions,
and have these additional definitions remain in force for the
duration of the browser's session? Alternatively, is there a
succinct way to tell the browser (at the beginning of a location
bar JavaScript micro-script) to load the definitions from this
module?

Thanks!

-Karl
 
L

Lasse Reichstein Nielsen

KKramsch said:
Increasingly often I find myself using brief JavaScript scripts
as pseudo-URLs in the location bar (i.e. using the javascript:
pseudoprotocol), particularly as bookmarks.

They are practical. They are usually called either "bookmarklets" (or
"favlets" since IE decided to call their bookmarks for "Favorites").
It would be great if these mini-scripts had access to utility
functions previously defined by me in a general utilities module.
Is there any way to tell the browser to "extend" its repertoire of
JavaScript functions, and have these additional definitions remain
in force for the duration of the browser's session?

That is, to synamically load a Javascript file after the page has
loaded. That is possible, in most recent browsers. Since you are using
bookmarklets for yourself, you have control over which browser you use,
so as long as it works for you, that should be sufficient :)

To load a Javascript file, you can do:

function load(surl) {
var s = document.createElement("script");
s.src = surl;
document.body.appendChild(s);
}

This should load the script file, eventually. The problem is that you
don't know when it is done. For that, I recommend adding a single line
to the end of the library:

if (window.callback) { window.callback(); }

Then you can make a bookmarklet:

javascript:(function(){window.callback=function(){delete window.callback;/*what you want to do*/};var s=document.createElement("script");s.src="libraryUrl.js";document.body.appendChild(s);}())

Or, you could just make one bookmarklet to load the library, and let
the others expect it to be there. Then you have to load the library
before using the rest. It might be necessary if using IE, which has
a 500 character limit on bookmarklets.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top