JavaScript Bookmark URLs (Advice)

Bookmarks composed of JavaScript calls.

You can build a bookmark by typing in a JavaScript URL. You do need to be careful as you type it in.

To create a JavaScript bookmark, open the bookmark manager and create a new entry. Be careful to only refer to objects that are likely to exist regardless of the page on view. You can crash the logic of the browser if your bookmark script tries to manipulate objects that belong to a page other than that which you are browsing.

It is necessary to work within these limitations because the user may request a bookmark at any time.

These JavaScript-coded bookmarks are sometimes known as Bookmarklets.

It is possible to create bookmarklets to aid in a number of information-gathering and debugging tasks. The example demonstrates how to display form elements in a pop up window. This only works if your bookmarks can hold a long enough URL value. It works fine in MSIE and Netscape 6.0, but not in version 4.0 of Netscape although you can paste it into the location bar and hit return for the same effect.

Example code:

   // Provided for our enjoyment by Jon Stephens
   // Note, the line breaks and space have been added to aid
   // readability.  You should omit the line breaks when
   // inserting this into the bookmark. Any unnecessary spaces
   // can also be removed to prevent URL buffer overruns.
   // This also works fine when wrapped in a function
   // declaration and called from a button on the form page.
   javascript:
   var output = "";
   for(var i = 0; i < document.forms.length; i++)
   {
   for(var j = 0; j < document.forms[i].elements.length; j++)
   {
   output += "Form "
   +  i
   +  " -- # "
   +  j
   +  " -- Type: "
   +  document.forms[i].elements[j].type
   +  "; "
   +  document.forms[i].elements[j].name
   +  ": "
   +  document.forms[i].elements[j].value
   +  "<br>"
   }
   };
   var newWin = window.open("","newWin","width=350,height=350");
   with(newWin.document)
   {
   open();
   write(output);
   close();
   }

See also:Bookmarklets, JavaScript interactive URL, javascript: URL

Cross-references:

Wrox Instant JavaScript - page - 49