Google Answers Logo
View Question
 
Q: JavaScript bookmarklet (IE): embedding and then calling a function ( No Answer,   6 Comments )
Question  
Subject: JavaScript bookmarklet (IE): embedding and then calling a function
Category: Computers > Programming
Asked by: mazzer1996-ga
List Price: $10.00
Posted: 21 Nov 2005 14:41 PST
Expires: 21 Dec 2005 14:41 PST
Question ID: 595949
I'm trying to create a bookmarklet for IE. The bookmarklet works
perfectly in Firefox; the only reason it chokes in IE is because of
that browser's limit on bookmark length. Therefore, I am linking to a
javascript file on my server that contains a function, and I would
like to call that function from the bookmarklet once the functions
have loaded. (The reason I do not call the function from within the
javascript file is because I need to pass a parameter to the
function.)

The function I want to call is named "doIt()". The following
bookmarklet seems like it *should* work--if I replace "doIt(x)" with a
simple alert box, it triggers--but somehow the scope isn't right, no
matter what I try.

Other variations include calling the function at the end of the
bookmarklet, but it will only run on the second pass of the
bookmarklet, after the script has already been added to the page
twice. (The first time it generates an "object expected" error.)

Any help you can provide would be much appreciated. I will paste the
bookmarklet below; I've spaced the code out for legibility instead
keeping it all on one line as required by bookmarklets.


Bookmarklet:
------------
s=document.createElement('script');
s.id='fs';
s.type='text/javascript';
s.language='javascript';
s.onload=(function(){doIt('value');})();
s.src='http://example.com/file.js';
document.body.appendChild(s);
void(null);

Clarification of Question by mazzer1996-ga on 22 Nov 2005 08:56 PST
I have determined that adding a settimeout of at least 0.5 seconds
(500ms) will solve the problem, as below:

s.onload=(function(){setTimeout('doIt(\'value\');',500)})();

However, this is undesirable. For one thing, it is not clear what
problem the settimeout solves. Is it outlasting the load time for the
"doIt" function--i.e., is the onload method not really waiting for the
function to load? Or is it outlasting the *Internet* load time--i.e.,
the time it takes to grab the script from my server? Either way, given
a sufficiently slow processor or Internet connection, any timeout
value will not be sufficient, and becomes a nuisance to the user even
when it is.
Answer  
There is no answer at this time.

Comments  
Subject: Re: JavaScript bookmarklet (IE): embedding and then calling a function
From: sipher-ga on 29 Nov 2005 22:04 PST
 
I can only think of a couple possible solutions.

1) Append the values you want to pass to the src attribute. Instead of
"http://example.com/file.js" it would become
"http://example.com/file.js?foo,bar".

Then at the end of file.js you would retrieve them with something like -

myscript = document.getElementById('fs');
myargs = myscript.split('?')[1].split(',');

2) Pretty much thesame as 1 but just output the js from a server side
script that grabs the args.

You of course now call doIt from the end of file.js.

- Damon
Subject: Re: JavaScript bookmarklet (IE): embedding and then calling a function
From: mazzer1996-ga on 30 Nov 2005 06:30 PST
 
Hi Damon,

I thought of that, too. That would probably work except that the
arguments I want to pass are potentially sensitive, so I don't want
them to be transmitted plaintext over the internet.
Subject: Re: JavaScript bookmarklet (IE): embedding and then calling a function
From: sipher-ga on 04 Dec 2005 13:12 PST
 
That's an easy one.. just use a hash sign instead of a question mark.
Doesn't include that bit in the uri when making a request to the
server.
Subject: Re: JavaScript bookmarklet (IE): embedding and then calling a function
From: mazzer1996-ga on 04 Dec 2005 14:45 PST
 
It's still transmitted plaintext, though: no encryption. Anyone
sniffing can see it in the clear.
Subject: Re: JavaScript bookmarklet (IE): embedding and then calling a function
From: sipher-ga on 04 Dec 2005 15:26 PST
 
I'm not getting you...

It's not transmitted anywhere. Not that part of the URI. 

Take this example for instance -

"http://example.com/foo.html#someStringData"

All that would be actually sent anywhere is "/foo.html". The hash sign
is only used by the browser to say "jump to this portion of the page"
and is stripped from any requests made.

If you get the "Live HTTP Headers" or "Tamper Data" extensions for
Firefox, you can see exactly what's sent.

If I'm missing something here, let me know.

If you want faster help than these comments, you could try the IRC
channel I hang out in on freenode (irc.freenode.net) in #web
Subject: Re: JavaScript bookmarklet (IE): embedding and then calling a function
From: mazzer1996-ga on 16 Dec 2005 14:15 PST
 
Got this working. There was a slight error in your code, however; should be:

myscript = document.getElementById('fs').src;
myargs = myscript.split('?')[1].split(',');

Note the ".src".

Thanks for the help.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy