0

For some reason IE won't trigger an onclick event. I have a link in my webpage which should renew a captcha image, but no matter what I try, the onclick event won't trigger. I even tried this to test the onclick event:

<a href="#" id="rc" onclick='alert("test"); return false;'>change image</a>

But nothing happened. I also tried to add the onclick event using js in the window.onload event, same result. All other javascript scripts do work, so js is working. Does anyone has any idea why this doesn't work?

by the way, the event doesn't work in any version of IE, and it does work in any other browser.

Edit: If you want to see the full source, go to: http://www.rosegardenvoorburg.nl/Contact?stackoverflow The page is in Dutch, but the sourcecode is (of course) HTML, so you must be able to understand that.

edit2: I've found a solution myself, and you're never gonna believe what's wrong: When I'm logged in to the control panel, a div is added at the top of the page, similar to the one shown in ie7 (which tells you you're browser is too old). However, when I don't add a border to that div, the captcha refresh button doesn't work. This doesn't make any sense at all, but at least I've found a solution...

Tiddo
  • 6,331
  • 6
  • 52
  • 85
  • 1
    Must be something else going on in either your code or your configuration because that code is perfectly valid and works in IE (just tested to make sure I didn't miss something silly). – squidbe Jun 11 '11 at 08:33
  • 1
    It works in IE8 and IE9 on my machine. please send a [sscce](http://sscce.org/) – Kerem Baydoğan Jun 11 '11 at 08:35
  • probably the onclick even doesn't trigger cause there's an error somewhere else in your script. – LeftyX Jun 11 '11 at 08:39
  • @squidbe @kerem @leftyx @mplungjan I've added a link to the page so you can check the source. Btw, the link itself is working (when I click it, the # is added to the end of the url), only the onclick event isn't triggered for some reason. – Tiddo Jun 11 '11 at 08:57
  • It works for me in IE8/7 -- captcha refreshes. However, I am seeing an error on this line: var dinges = document.getElementsByClassName("googlemaps") -- I assume since getElementsByClassName() is not a native IE function. If you're sure you have javascript enabled, it might be time for a reboot. :-) – squidbe Jun 11 '11 at 09:19
  • @squidbe Hmm, it seems that it only doesn't work when I'm logged in to the control panel, which adds an extra bar on top of the page. However, I can't allow you to login to the control panel for obvious reasons. I'll try to post the source code of it in a few minutes – Tiddo Jun 11 '11 at 09:29
  • Aha - so it DOES work but not if you are logged in.. Hard for us to guess ;) http://stackoverflow.com/questions/900117/is-getelementbyclass-safe-to-use-across-browsers-like-getelementbyid – mplungjan Jun 11 '11 at 09:57
  • @mplungjan @squidbe I've fixed the getelemensbyclassname thing and I've also fixed my problem. I'll provide the solution above in my question. – Tiddo Jun 11 '11 at 10:00

3 Answers3

1

Try with the below:

<a href="javascript:void(0);" id="rc" onclick='alert("test"); return false;'>change image</a>

Also have a look at Which "href" value should I use for JavaScript links, "#" or "javascript:void(0)"?

Community
  • 1
  • 1
adarshr
  • 61,315
  • 23
  • 138
  • 167
1

You are doing it in the wrong order

FIRST add the onload, THEN change the source

var cImg;
function renewCaptcha(){
  cImg = new Image();
  cImg.onload=function(){document.getElementById("captcha").src = cImg.src;};
  cImg.src='/Img/captcha/securimage_show.php?' + Math.random();
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • as I said before, the onclick event isn't even triggered: if I replace the whole function with alert('hi') nothing happens either. So even if this is a problem, it's not the reason why the event isn't triggered. edit: tested, problem still exists. However, thanks for mentioning this! – Tiddo Jun 11 '11 at 09:06
0

Few tips to ponder!

  • check if javascript:alert("test") on your ie address bar pops up the message.
  • Also check and make sure that javascript option is not turned off.
  • You may also want to reset your ie settings and see if it work.
  • Also try to see if same works on your fellow colleague's computer.
Prashant Bhate
  • 10,907
  • 7
  • 47
  • 82