3

Hi I'm trying to make a javascript bookmarklet that will add a link to an external javascript source to a page that is off the domain. However nothing happens when I run the bookmarklet no errors and the code on the page never changes. Any ideas? Here is the bookmarklet I'm trying to use. Thanks for your time.

javascript:(function(){document.body.appendChild(document.createElement('script')).src='http://mycode.com/autopopulator.js';autopopulate();})(); 
1
  • 2
    You're probably correctly appending the script tag. I suspect the problem is that you have to wait for the browser to load the script before that function call can work. Commented Oct 10, 2010 at 21:14

3 Answers 3

2

You can also achieve this with a callback:

    var addScript=function(filename,callback){
            var e=document.createElement('script');
            e.type = 'text/javascript';
            e.src = filename;
            if(callback){
                e.onloadDone=false;//for Opera
                e.onload=function(){e.onloadDone=true;callback();};
                e.onReadystatechange=function(){
                    if(e.readyState==='loaded'&& !e.onloadDone){
                        e.onloadDone=true;callback();
                    }
                }
            }
        if(typeof(e)!=='undefined'){
            document.getElementsByTagName('head')[0].appendChild(e);
        }
    }
addScript('http://yoursite.com/js/yourScript.js',function(){functionFromYourScript();});

(of course you'll want to optimize this to cram it in a bookmarklet, but you get the idea...)

Sign up to request clarification or add additional context in comments.

1 Comment

heh, turns out I needed it myself, so here's the compressed version (you just need to add your function call with callback to the end of this) javascript:var www=function(f,c){var e=document.createElement("script");e.type="text/javascript";e.src=f;if(typeof(e)!=="undefined"){if(c){e.onloadDone=false;e.onload=function(){e.onloadDone=true;c()};e.onReadystatechange=function(){if(e.readyState==="loaded"&&!e.onloadDone){e.onloadDone=true;c(e.innerHTML)}}}document.getElementsByTagName("head")[0].appendChild(e)}};
0

I've never attempted to create a bookmarklet. But I have found this example online which incorporates jQuery which could be of some use to you.

http://www.latentmotion.com/how-to-create-a-jquery-bookmarklet/

It explains on how to link to external JS files and incorporate other files within that, but I think the kind of thing you are looking for is:

<a href="javascript:(function(){var head=document.getElementsByTagName('head')[0],script=document.createElement('script');script.type='text/javascript';script.src='http://www.site.com/your-javascript.js?' + Math.floor(Math.random()*99999);head.appendChild(script);})(); void 0">Your Bookmarklet Name</a>

1 Comment

That still doesn't show any changes. It is as if it isn't changing anything on the page at all.
0

You need to add code that

  1. inserts the required script tags and then
  2. use a timer interval to repeatedly check for an object from the imported script

Here's sample code:

function writeTags(){
    //write the script tags
}
function check(){
    // example for prototype library
    if(window.Prototype && Prototype.Version){
        doActualWork();
    }else{
        window.setTimeout(check, 200);
    }
}
function doActualWork(){
    // this your actual code that requires
    // the loaded library
}
writeTags();
check();

2 Comments

I'm curious as to what I accomplish by checking for an object from the script?
if you find the object, you know the script is loaded and you can use it's methods. it's a common check in JavaScript when working with libraries

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.