Save $50 on Dreamhost Hosting - Any Plan

As a preferred visitor, you can get $50 off of any Dreamhost hosting plan by using coupon code CWD50 when you check out, for a limited time. Dreamhost is a reliable hosting provider with solid customer service and a proven track record.

Reliable Hosting With the Best Customer Support

Looking for a dedicated hosting solution with 24/7/365 U.S.-based, English speaking phone support? Visit Rackspace for a reliable, managed hosting solution that will let you focus on the bottom line - your business, while they make sure your website stays online.



Aug 29 2009

Bookmarklet Script


Filed under: Web Programming » Java Script,
Tools:

What is a Bookmarklet?

A bookmarklet is basically a software application for your browser. But what bookmarklets are most known for is their ability to run a website script from anywhere - in other words, the application is loaded in the browser, and doesn't have to be called from a particular web page. For example, if you visit our URL shortcut creator, LinkTh.at - you'll see a bookmarklet which will let you create shortcuts from any webpage, instantly - without having to copy the URL, leave the page, and enter it into the form on LinkTh.at. This creates a huge convenient to clients and proves to be a big time saver.

How Do I Create a Bookmarklet?

Developers are often interested in creating bookmarklets for their website applications or forms, as it will allow clients to use their service without having to load their website in a separate window. Common popular services that take advantage of the powers of a bookmarklet include del.icio.us, digg, and other social media networks, as well as various websites running scripts. We're going to show you the fundamentals to creating your own bookmarklet script.

Bookmarklet Script

With a bookmarklet, all you're doing is creating a bookmark that runs a script. The difference between the bookmark of a static URL and a bookmarklet, is that with the latter the URL consists of Javscript code, which can execute various functions. Therefore, for most scripts, you simply call the script using javascript:

javascript:<js commands>

As an example, we call 

javascript:alert('Sample alert message');location.href='http://www.example.com/new-page/'

The way you actually use this is by creating an anchor (clickable URL) out of the script - so users can right-click your link and create a bookmark out of it. Then, when they load the bookmark, it will load the javascript code and execute your script. Here's an example: Just an Alert. If you right-click this link and bookmark it, whenever you load up the bookmark you'll see the alert message.

Advanced: For bookmarklet scripts that only accept POST submissions, you'll have to create an invisible form and submit it like so:

javascript:myForm=document.createElement("form");
myForm.style.display="none";
myForm.method="post";
myForm.action="http://example.com/";
myInput=document.createElement("input");
myInput.setAttribute("name","description");
myInput.setAttribute("value",document.URL);
myForm.appendChild(myInput);
document.body.appendChild(myForm);
myForm.submit();
document.body.removeChild(myForm);

Have fun!

Average: 5 (1 vote)
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Textual smileys will be replaced with graphical ones.

More information about formatting options

CAPTCHA
This question is used to make sure you are a human visitor and to prevent spam submissions.