views:

258

answers:

2

Hi everyone. I've been tinkering around this application all day long and have had a few difficulties. So basically the bookmarklet needs to allow the user to use a lot of functions from multiple JavaScript files, although all of the functions that the user will use are called from one file. Basically it includes all of the necessary files in order to perform a certain function. It uses jQuery, and I've found that it's really hard to have jQuery running on your bookmarklet.

My question is: How can I load multiple javascript code files into your Bookmarklet?

Yours truly,
Emil Hajric.

A: 

you can embed the scripts to the document the bookmarklet is working on f.ex if your bookmarklet is something like

    javascript:void((function(){var e=document.createElement('script');
    e.setAttribute('type','text/javascript');
    e.setAttribute('charset','UTF-8');
    e.setAttribute('src','http://yoursite/bookmarklet.js');
    document.body.appendChild(e)})());

you can add as many script elements as you wish, try adding jquery in there.

zalew
A: 

If you have to run several JavaScript files using a bookmarklet I'd recommend creating a function in the bookmarklet to import files.

That function could be written as

var importJs=function(jsUrl){
  var s=document.createElement("script");
  s.setAttribute("src",jsUrl);
  document.body.appendChild(s);
};

In order to actually use in a bookmarklet, just declare that function, and then call it once for each file you have to import.

javascript:(function(){
  var importJs=function(jsUrl){
    var s=document.createElement("script");
    s.setAttribute("src",jsUrl);
    document.body.appendChild(s);
  };
  importJs("http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js");
  importJs("http://your.domain.com/codeThatUsesJQuery.js");
})();

(Note: To actually use this as a bookmarklet it is necessary to join all the lines into a single line.)

Peter Dolberg