Guilherme
Guilherme

Reputation: 623

Javascript function just works on Internet Explorer

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!

Upvotes: 0

Views: 255

Answers (6)

Jose Faeti
Jose Faeti

Reputation: 12314

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

Upvotes: 0

Guilherme
Guilherme

Reputation: 623

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!

Upvotes: 0

mplungjan
mplungjan

Reputation: 178421

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>

Upvotes: 0

Andrew
Andrew

Reputation: 13863

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

Upvotes: 2

SLaks
SLaks

Reputation: 888293

The click() method only exists in IE.

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

Upvotes: 2

rlb.usa
rlb.usa

Reputation: 15041

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.

Upvotes: -2

Related Questions