3

i wrote this bookmarklet

<a href="javascript:var m = document.getElementById('xxx'); m.value=17;">test</a>

and it is working in Chrome, but not in Mozilla Firefox

i even tried

<a href="javascript:var m = document.getElementById('xxx'); m.value='17';">test</a>

but it doesn't work too

what am i doing wrong?

alessio
  • 41
  • 2

2 Answers2

0

These are the steps I recommend for making bookmarklets:

1. Put everything in an immediately invoked function

You can use (function(){YOUR CODE}()); or (function(){YOUR CODE})();

2. Minify your code

You can use an online JavaScript uglifier

3. URL-encode it

You can use an online URL-encoder

In your case it could be something like this:

(function(){m=document.getElementById('xxx');m.value=17}());

encoded as:

(function()%7Bm%3Ddocument.getElementById('xxx')%3Bm.value%3D17%7D())%3B

See DEMO.

rsp
  • 107,747
  • 29
  • 201
  • 177
0

It is not working because FF expects to follow the link to somewhere. This piece of code works

<a href="javascript:void(document.getElementById('xxx').value=17)">test</a>

But consider making your code unobtrusive. This may help you

Community
  • 1
  • 1
Bakudan
  • 19,134
  • 9
  • 53
  • 73
  • You can't really have an unobtrusive bookmarklet. All of the code has to be `...`. It's a good advice fo any non-bookmarklet JavaScript link, though. – rsp Mar 12 '11 at 14:44
  • It`s not about the bookmarklet. It is in general - thus the code is easyer to read, maintain, reuse etc. – Bakudan Mar 12 '11 at 14:57