CSS & JavaScript Injection Bookmarklets

January 30, 2010 at 11:34 PMBen

I like using bookmarklets to inject scripts into a page I’m on.  One of the popular ones for this is the jQueryify bookmarklet to inject jQuery into a page.

After getting the jQuerify bookmarklet, I created a separate bookmarklet to inject my own JavaScript file into a document.  This is handy when I’m debugging a website I either don’t have the files for, or just want to test changes out within the confines of my own browser without touching any of the live files.  To do this, I simply Edit the bookmark in my browser, and change the URL embedded in the bookmarklet to point to the URL of the JavaScript file I want to inject into the page.  The URL to the script can even be a script on your own computer accessible via http://localhost, if you’re too lazy to upload the script to a public website :)

Recently, I needed to inject a CSS stylesheet into the page I’m on.  The bookmarklet for this is very similar to the JavaScript injection bookmarklet.  The bookmarklet I created for this is at the bottom of this post.

Dynamic URL Bookmarklets

Even though it’s not a big hassle to Edit the CSS/JS injection bookmarks to change the URL to the JS or CSS file embedded within the bookmarklet, I realized a very convenient bookmarklet would be one that would prompt me for the URL to the CSS/JS file, and then inject that URL.  By doing this, I don’t need to edit the bookmark, and can easily inject any CSS or JS file.  It’s also easy to inject multiple URLs by running the bookmark more than once, and entering a different URL each time.

Bookmarklets – For your Browser

For convenience’s sake, I have these 4 bookmarklets down below.  Feel free to use them.  Two of the bookmarklets are for JS injections and two are for CSS injections.  Within each pair, one bookmarklet has the URL already embedded within it, and the other one will prompt you for the URL.

Just drag these links into your Bookmarks toolbar or menu area.  For reference, the bookmarklet code is under each link.

> Inject JS file <

> Inject JS file (get prompted) <

> Inject CSS file <

> Inject CSS file (get prompted) <

Posted in: Development

Tags: , ,

JavaScript Events Not Firing

September 21, 2008 at 3:45 PMBen

It's important to realize a JavaScript event is not guaranteed to fire.  The events I have in mind are events such as onmouseout and onmouseover.  I'm sure there are many other events that also may not fire depending on the circumstances at the time the events would normally fire.  If the element is right up against the edge of the browser viewport when the mouse moves away from the element and out of the browser, or if the browser or computer is busy processing something else at the time the event would be expected to occur, it may just not happen.

While working with jQuery recently I created a rollover image with the hover() function to show a different image when the mouse rolls over the image.  The particular piece of code was:

  function(){ this.src = this.src.replace('.gif','_over.gif'); },
  function(){ this.src = this.src.replace('_over',''); }

This code works good assuming your rollover image has the same filename as the original image with "_over" at the end of the filename.  The problem occurred one time while testing the page.  I noticed the image replacement was not occurring and I was getting some ugly flickering when hovering over the image.  Fortunately, I had Firebug already turned on and checking the Net tab showed a red item indicating a 404 error.  The file that the browser was getting a 404 on had this filename:


Apparently, one of the previous times I had rolled over the image, for whatever reason the onmouseout event didn't fire leaving the images source as myImage_over.gif (the rollover image).  The next time I rolled over the image, the onmouseover event fired changing the image's source from myImage_over.gif to myImage_over_over.gif.  Not good!  A very easy and effective solution in this case was to make sure a preexisting "_over" in the image source was removed prior to adding it back in the onmouseover event handler.

  function(){ this.src = this.src.replace('_over', '').replace('.gif','_over.gif'); },
  function(){ this.src = this.src.replace('_over',''); }

This is a much more robust method of making sure things don't fall apart in the rare event a JavaScript event doesn't fire.  Although rollover images are not usually critical to a page's functionality, it's still a bad experience to the end user if they see things flickering that shouldn't be flickering.  This also makes you realize that for any client side code, it's important to not assume events will always fire and be sure your code is defensively designed so it can recover from unexpected situations.

Posted in: Development

Tags: ,