mcottondesign

Loving Open-Souce One Anonymous Function at a Time.

How to write a bookmarklet

Problem: I have to use a punch clock webapp for work. It doesn't have jQuery already included and I would like it to work from the iPhone.

Solution: Write a bookmarklet in javascript. Fill-in the text fields and click the submit button.

(function(){  
  // Grab the username and password input fields
  user = document.getElementById('txtUserName'); 
  pass = document.getElementById('txtPassword');

  // Grab the submit button, oddly named 'punch'
  punch = document.getElementById('btnPunch'); 

  // Assign them values and simulate clicking the 'punch' button
  user.value = 'foo'; 
  pass.value = 'bar';
  punch.click()

// Wrap it up in parens so it is a self-calling function
})();

Now you need to make a new bookmark and edit the URL to look like this:

 javscript:<function goes here>

In mobile safari, use the action button (square box with an arrow escaping) to add the original URL to your home screen. Once the page loads you will open up your saved bookmarks and choose this bookmarklet. You can find it and fork it on github [https://gist.github.com/1332353]