0

Let me introduce another of IE's tricks!

Today, to my surprise I have the inverse situation. I have something working well on Internet Explorer and not on FireFox and Chrome! Seriously, does someone know why this is functional in IE and not on other browsers? This code seems useless, but that's because I've isolated the problem. Here we go:

<html>
<head>
<script>
function redirectFuncionTest(value){
document.getElementById(value).click();
return false;
}
</script>
</head>
<body>
    <input type="button" value="start redirectFuncionTest"
        onclick="redirectFuncionTest('idLink')" />
    <a id="idLink" name="namelink" href="http://www.google.com"></a>
</body>
</html>

Any help? Is there another way to do something like that?

Thanks for read!

ayyp
  • 6,590
  • 4
  • 33
  • 47
Guilherme
  • 593
  • 1
  • 6
  • 23
  • possible duplicate of [how do I programmatically click on an element in firefox?](http://stackoverflow.com/questions/809057/how-do-i-programmatically-click-on-an-element-in-firefox) – mplungjan Aug 03 '11 at 19:59

6 Answers6

2

The click() method only exists in IE.

Instead, you can write location = document.getElementById(value).href

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

It is difficult, see the previous question on the same topic,

How do I programmatically click a link with javascript?

I recommend using jQuery or some other library if it is available. Alternatively, just use

window.location = link.href;

And bypass the link

Community
  • 1
  • 1
Andrew
  • 13,757
  • 13
  • 66
  • 84
  • @mplungjan It's never "needed", it is however easier and less error prone and browser agnostic. Making everything simpler. – Andrew Aug 03 '11 at 19:52
0

Here How do I programmatically click on an element in JavaScript?

<html>
<head>
<script>
if(typeof HTMLElement!='undefined'&&!HTMLElement.prototype.click) {
HTMLElement.prototype.click=function() {
var evt = this.ownerDocument.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
this.dispatchEvent(evt);
}
}
function redirectFuncionTest(value){
  document.getElementById(value).click();
  return false;
}
</script>
</head>
<body>
  <input type="button" value="start redirectFuncionTest"
    onclick="redirectFuncionTest('idLink')" />
  <a id="idLink" name="namelink" href="http://www.google.com"></a>
</body>
</html>
Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

click() is not a JavaScript method. Probably IE implements it as a custom IE method.

Some libraries such as jQuery have that method.

click() (javascript) method is not working in FF

Community
  • 1
  • 1
Jose Faeti
  • 12,126
  • 5
  • 38
  • 52
0

Ok, with your help i wrote something like this snippet who solved my problem...

<html>                                                                  
 <head>                                                                  
 <script type="text/javascript" src="jquery.js"></script>          
 <script type="text/javascript">                                         
    $(document).ready(function() {
   $("#btn_go").click(function(){
   $(location).attr('href',"http://www.google.com");

   });
 });                                    
 </script>                                                               
 </head>                                                                 
 <body>       
    <input type="button" id="btn_go" value="go" />
 </body>                                                                 
 </html>

Thanks for everyone, you guys are awesome!

Guilherme
  • 593
  • 1
  • 6
  • 23
-2

Culprit

document.getElementById...

is your culprit here, it's not very cross-browser safe. I know it makes learning VERY hard.

Solution : Javascirpt Utility Function

Here's a cross-browser solution.

  function get_object(id) {
   var object = null;
   if (document.layers) {
    object = document.layers[id];
   } else if (document.all) {
    object = document.all[id];
   } else if (document.getElementById) {
    object = document.getElementById(id);
   }
   return object;
  }

Alternate Solution: JQuery

If you'd really rather have a simpler solution, give JQuery a quick glance. It's something new to learn, but it is much more cross-browser compatable than out-of-the-box javascript.

Community
  • 1
  • 1
rlb.usa
  • 14,942
  • 16
  • 80
  • 128
  • 4
    No browser less than 15 years old has any problem with `getElementById` – SLaks Aug 03 '11 at 19:47
  • Only IE allows to use ID on a named button without ID – mplungjan Aug 03 '11 at 19:49
  • document.getElementById() nowadays is safe in every browser, and is what jQuery uses to get elements by ID too, since you are advising to use a JS library only for retrieving elements by ID, the simpler of the tasks. – Jose Faeti Aug 03 '11 at 19:54
  • 2
    The code you posted works wonderfully with Netscape 4 and IE 5. Very useful ;) – mplungjan Aug 03 '11 at 20:08
  • @SLaks: Close. Netscape 4 and IE 4, both released in 1997, didn't have `document.getElementById()`, and Netscape continued releasing 4.x versions into the 2000s. But yes, you're obviously right that no browser relevant today doesn't have support for it, although it's worth mentioning that the implementation is flawed in IE up to and including version 7. – Tim Down Aug 03 '11 at 23:57
  • @Tim: How is it flawed? Does it return `name`d elements? – SLaks Aug 04 '11 at 00:20