开发者

Javascript function just works on Internet Explorer

开发者 https://www.devze.com 2023-03-25 06:25 出处:网络
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

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>
fu开发者_JAVA百科nction 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!


The click() method only exists in IE.

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


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


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>


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!


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消