Skip to content
Advertisement

Simple bookmarklet not working in chrome

I am new to bookmarklet coding and i have run into a problem where the regular javascript works fine in the browser but not the bookmarklet version.

I had found a bookmarklet which finds a image and turns it into BB code and that works fine, however it loads jQuery and i did not want it doing that all the time. So i was basically just trying to remove the need for it but now it does not work anymore as a bookmarklet.

The original bookmarklet was this:

JavaScript

My javascript which works as regular JS code is:

JavaScript

When i add javascript: and append the minimized code i can not get it working no matter what i try. If anyone could help me get this working that would be a big help, thanks.

Advertisement

Answer

Yeah, you have an error in your code. I would like to show you how to find it yourself rather than just say where it is, so here goes a micro-tutorial 😉

  1. Strip line comments (//) from your JS code as the bookmarklet will be on one line. You should end up with the code:

    JavaScript
  2. Remove new line characters from the code and prefix the code with javascript: protocol so that Chrome knows that the contents is JavaScript code. Your code should now look like this:

    JavaScript
  3. Create your bookmarklet as a bookmark and make sure its accessible (i.e. visible so you can click it in any Chrome window).

  4. Go to a page on which you will test your bookmarklet code.
  5. Open Developer tools by holding CTRL + SHIFT + I.
  6. Switch to the tab named Console.
  7. Click the bookmarklet.
  8. The console should now show you the errors/warnings that have been issued while executing the code of your bookmarlet.
  9. In case of your code, it spits:

    JavaScript

Which seems reasonable since search() is invoked on the value of str which contains a value of ID attribute, but not all IMG tags have an id assigned.

What’s cool is that you can click on the (program):1 on the right hand side of the window and debug the code, inspect variables, etc.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement