Javascript saves the day again.

Categories

Javascript

Tags

General Techie Stuff

As some of you know, one of my hobbies is electronics with micro controller usage, and I happen to use an electronics forum a lot when I’m losing hair excessively.

One of the functionalities I really miss on this forum (vBulletin), was an option to be able to see the code posted in a bigger version, not by magnifying it, but upsizing the code box, so I didn’t have too much scroll.

I then remembered that GreaseMonkey can be an excellent tool for this cases. I could just talk to the forum admin to see if they could change it, but as not everybody would like the change, I decided to write my own custom script and integrate it into greasemonkey.

Everything seemed to be pretty perfect, except for the fact that greasemonkey doesn’t really like to call functions directly, but needs to have them added with addEventListener().

Shouldn’t be a problem (besided being my first time using this function with JS), except for the fact that addEventListener() is not as straight forward to work as it seems to be.

If you have it on a loop like this:

for(vari=0;i<5;i++){<!---yourlistenerhere--->}

Youd probably be tempeted to add your code like this:

myLink.addEventListener("click",function(e){toggle(i);},true);

In case you want to pass the index as an argument to the UDF toggle()

The funny thing is that once it’s completely rendered, you will always have the value “6” being passed on your link.

This really gave me a headache, as I couldn’t understand why in hell the value “6” was passed. In fact, it happens exactly because your code is all rendered, and you don’t have the values from the loop to be working with.

The workaround for this is to use the object id instead of the loop index.

In this case, my links are being created dynamically, and having an id assigned with each iteration of the loop. So by using it like this: