Home | Hardware | Internet News |Web Hosting |IT Management |Network Storage
LinuxPlanet
Search 
  Power Search | Tips 

 Front Door
 Discussion
 LinuxEngine
 Opinions
 Reports
 Reviews
 Tutorials
 News
 Technology Jobs

 Browse by subject.
Free Newsletter

Java/Open Source Daily
Linux Today
More Free Newsletters

Be a Commerce Partner


















internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

Print this article
Email this article

   LinuxPlanet / Tutorials







Roll Your Own Custom Bookmarklets In Firefox, part 2
Sandwiches, Functions, and More Bookmarklets

Akkana Peck
Thursday, October 23, 2008 11:25:40 AM

You can get bookmarklets for all sorts of specialized functions. There's an especially rich choice for web developers: bookmarklets to outline block elements, show or change a page's CSS, or validate HTML. See the links at the end of this article for some good bookmarklet sources.

If you know some Javascript, you can write your own bookmarklets. Just use the same techniques you'd use for Javascript on a page, with a couple of exceptions. First, you have to cram it all into one long "URL encoded" line, without line breaks, spaces or other characters that aren't allowed in a URL.

Return values are also important. Javascript that doesn't return anything, or returns an undefined type, will keep the current page loaded. If you return some other type, such as a string, the browser will load that string as a new HTML page. That's one way of loading a new page; another way is to set document.location to a new URL, like the Wikipedia and Babelfish bookmarklets in this article. Most bookmarklet authors sandwich their Javascript inside an "anonymous function", like this:

javascript:(function() { /* Bookmarklet code here */ })();

This also gives you the advantage of variable scoping: any variables you declare with var inside this function won't conflict with variables of the same name defined on the current page. Some people prefer to use if(1){ ... } around their bookmarklet code. Your choice.

You can do a lot with bookmarklets, whether you're writing them yourself or just using existing ones. Here are a few to start with:

Akkana Peck is a freelance programmer whose credits include a tour as a Mozilla developer. She's also the author of Beginning GIMP: From Novice to Professional.

« Back: Javascript Bookmarklets

Skip Ahead

1 Javascript Bookmarklets
2 Useful Shortcuts Galore
3 Sandwiches, Functions, and More Bookmarklets





Linux is a trademark of Linus Torvalds.


internet.com home | search | help! | about us

Jupiter Online Media

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers