Wednesday 24 October 2012

Bookmarklets and closing a window with js

I want to allow users to update one application (LIMS) with basic information whilst the product generating the output was still running.

Prior to this, the finished output is seen by a daemon, which uses an api and creates the information into the application, and users then update.

The problem is that the users are using the product's own software, and we don't want to modify that to use the api to post, so what do we do?

Solution: create a bookmarklet

I hadn't come across bookmarklets before this week, as I just haven't needed them. But it is simply a bit of javascript that you drop into you bookmarks toolbar, and it deals with loading a page from a website, maybe scraping from the one you are on (see delicious.com) and giving a pop-up window, or overlays the page with additional features that you might want, such as ability to draw on the page.

So, this enables me to load into a popup, a cutdown form from the external application, screen scraping for a filename, and then closing once saved.

Here is an example of a bookmarklet (drop it into a href attribute on a link, and you can then drop it into bookmarks easily) NB: I am using Template Toolkit to fill in SCRIPT_NAME/class/method

  javascript:(function()%7Bf='[% SCRIPT_NAME %]/[% class %]/[% method %]
  ?nodecor=1&filename='+document.getElementById('filename').val+'&';
  a=function()%7Bif(!window.open(f+'noui=1&jump=doclose','',
  'location=1,links=0,scrollbars=1,toolbar=0,width=1100,height=600'))
  location.href=f+'jump=yes'%7D;if(/Firefox
  /.test(navigator.userAgent))%7BsetTimeout(a,0)%7Delse%7Ba()%7D%7D)()

To close the window once it had successfully saved out the data was interesting. Javascript doesn't have an actual sleep function, and many many people were commenting 'Why would you want one?'

Well folks, the reason is that sometimes, sleeping is convenient. I want people to have the chance to be told their data is saved, but close the window down and get it out of their face, to save them having to do it.

It turns out in this case that I could just use the setTimeout function, as below (jQuery is utilised)

  $(document).ready( function () {
    setTimeout( function () { window.close() }, 5000 ); } );

So, now I have a working support for the users, who can better utilise the time that they are waiting for the product to produce it's output, and save data more upfront.


1 comment:

Unknown said...

Outsource Software Development OTS Solutions : ISO 9001 & Microsoft Certified Software Outsourcing & Development service provider company offering software Application development , IT outsourcing services to its clients across the globe.