ExpatTech is currently short-staffed due to increased corporate on-site work, and our office will often be closed. Please call +36705578849 to arrange an appointment time!

ExpatTech Techblog

Peter Todd 2010.10.22. 12:43

Browser bookmark containing javascript

We can do a nice little tweak with the bookmarks. Bookmarks are usually used as bookmarks: they point to a web address. Browsers nowadays support add-ons or extensions and there is a huge palette of these nice little utilities helping us browsing faster and spend our time on the territory of world wide web. But for some people these add-ons (or extensions) are suspicious or some people cannot find the one they need for a task. A webprogrammer writes his/her own in this case, but what if the task we want to do is far easier then sitting down and struggling with an add-on (or extension).

Here comes javascript and bookmarks to the picture. When you add a bookmark, the browser saves the url and waits until you release the bookmark and navigates you to the page. But there is another obvious usage of a bookmark: add javascript code instead.

Let me explain this further. Let's say you want to send the actual page in an e-mail to a friend. Then you open up the e-mail client, copy-paste the webpage and send it.

If you do this many times, this procedure can be optimised with a bookmark containing javascript.

To write the example above, click on the browsers bookmark bar (or go to the bookmark manager, or edit an existing one) and add a new bookmark. Name it as you want to (I named it EMAIL THIS URL) and instead of adding a url to the location add this code:

javascript: win = window.open("mailto:?subject=Check this URL&body=" + window.location.href, "emailWindow"); if (win && win.open &&!win.closed) win.close();

This code creates a new window in the browser which opens the default mail client with the paramateres defined above. The body of the e-mail will be window.location.href, which is the url of the page you are actually visiting.

After this bookmark is added, browse to any page on the world wide web and press the button. It will open up your e-mail client with filled in subject and body.

Think about other functions aswell. This option is not as cool as the extensions, but fast and easy to create.

Note: the javascript code above works in Firefox.