By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,474 Members | 1,283 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,474 IT Pros & Developers. It's quick & easy.

add js client side

P: n/a
Is it possible to add a js function to a loaded web page on the client side?
E.g. IE is viewing google, can I add a javascript function to that instance
of IE viewing google? I am trying to hook browser events such as button
clicks and get information such as edit field text.

Thanks IA, Simon
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ivo
"Simon" <si****************@btinternet.please.com> wrote in message
news:c1**********@sparta.btinternet.com...
Is it possible to add a js function to a loaded web page on the client side? E.g. IE is viewing google, can I add a javascript function to that instance of IE viewing google? I am trying to hook browser events such as button
clicks and get information such as edit field text.


With your webbrowser parked at your preferred google page, type this in the
addressbar:

javascript:function x(){p=prompt('Your name...');alert('Welcome to Google,
'+p) }; void(x());

and hit return. You won't leave the page (if you didn't make a typing
mistake).
As you can see, you have just added a function called x to the current page.
It 's the type of scripting upon which bookmarklets are based. It will work
with any site. But it will only work for that specific copy of the page in
your browser and only until you leave the page. SO it will only be your own
clicks that you will be able to monitor.
And Internet Explorer 6 has reduced support for it: your code should not be
longer than 208 (or was it 255?) characters or it will be ignored. A
workaround in modern bookmarklets is to dedicate those 208 characters to
inserting a .js file on the fly, with something like
z=document.createElement('script');
z.src="http://mysite.com/myscriptonyoursite.js"
HTH
Ivo


Jul 20 '05 #2

P: n/a
> With your webbrowser parked at your preferred google page, type this in
the
addressbar:

javascript:function x(){p=prompt('Your name...');alert('Welcome to Google, '+p) }; void(x());

and hit return. You won't leave the page (if you didn't make a typing
mistake).
As you can see, you have just added a function called x to the current page. It 's the type of scripting upon which bookmarklets are based.


Thanks for that - looks useful. I want to make the process automatic (i.e.
without typing in the address bar) - any ideas? I guess I can probably hook
the address bar and update it but I'd rather use a neater method if you know
of one?

Simon
Jul 20 '05 #3

P: n/a
Ivo
"Simon" <si****************@btinternet.please.com> wrote in message
news:c1**********@titan.btinternet.com...
With your webbrowser parked at your preferred google page, type this in the
addressbar:

javascript:function x(){p=prompt('Your name...');alert('Welcome to

Google,
'+p) }; void(x());

and hit return. You won't leave the page (if you didn't make a typing
mistake).
As you can see, you have just added a function called x to the current

page.
It 's the type of scripting upon which bookmarklets are based.


Thanks for that - looks useful. I want to make the process automatic (i.e.
without typing in the address bar) - any ideas? I guess I can probably

hook the address bar and update it but I'd rather use a neater method if you know of one?

Simon


I got a present for you, Simon. Like a proper present, you need to carefully
unwrap it, that is copy and paste each of the following lines in your
address bar, one after another, without spaces or other noise in between
(meanwhile, look at the code and guess what is going to happen), so that it
becomes one line again:

javascript:void((function(){var s='';(function(w){for(var
i=0;i<w.length;i++){arguments.callee(w.frames[i]);}try{s+=w.document.selecti
on.createRange().text;}catch(e){try{s+=w.document. getSelection();}catch(e){a
lert('Bookmarklet failed:\n\n'+e.message);}}})(top);if(!s)s=prompt(' Enter
keywords:','');if(s)location='http://www.google.com/search?q='+escape(s);})(
))

If you 're confident all is well, hit return and if you haven't selected any
text on the current page, you will be prompted for a search query. You can
save this javascript code in your Favorites, on your Personal Toolbar even,
if you write it in an <a href="javascript:..." tag and bookmark that link.
There are other, more convenient ways but there re not all easy to explain
unless bookmarklets tickle your imagination.

Also see:
http://www.bookmarklets.com/tools/search/ introduces you to dozens of
bookmarklets, specifically engineered for faster searching.
http://www.bookmarklets.com/mk.phtml even has a bookmarklet to helps you in
creating bookmarklets for any search engines not yet listed.

HTH
Ivo
Jul 20 '05 #4

P: n/a
> I got a present for you, Simon. Like a proper present, you need to
carefully
unwrap it, that is copy and paste each of the following lines in your
address bar, one after another, without spaces or other noise in between
(meanwhile, look at the code and guess what is going to happen), so that it becomes one line again:

javascript:void((function(){var s='';(function(w){for(var
i=0;i<w.length;i++){arguments.callee(w.frames[i]);}try{s+=w.document.selecti on.createRange().text;}catch(e){try{s+=w.document. getSelection();}catch(e){a lert('Bookmarklet failed:\n\n'+e.message);}}})(top);if(!s)s=prompt(' Enter
keywords:','');if(s)location='http://www.google.com/search?q='+escape(s);})( ))

Thanks - that's cool :)
Is there a way that a bookmarklet can be run with a link. For example, some
PC has a link that points to a web app - something like http://192.168.1.22.
Can the bookmarklet be appended to the link somehow so that the web site
opens and the bookmarklet runs?
Also see:
http://www.bookmarklets.com/tools/search/ introduces you to dozens of
bookmarklets, specifically engineered for faster searching.
http://www.bookmarklets.com/mk.phtml even has a bookmarklet to helps you in creating bookmarklets for any search engines not yet listed.


I had a look at those and also http://tantek.com favelet - I'll need to
study them for a while.

Simon
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.