开发者

onClick parent getElementById.click(); not working in CHROME

开发者 https://www.devze.com 2023-02-11 00:43 出处:网络
I\'ve been trying my best to solve this problem but I really can\'t find开发者_JAVA百科 what\'s wrong. I\'m not really experienced with javascript so it\'s probably something basic I\'m missing.

I've been trying my best to solve this problem but I really can't find开发者_JAVA百科 what's wrong. I'm not really experienced with javascript so it's probably something basic I'm missing.

Right now, after I copied the code I found to allow browsers besides IE to register click(); it works perfectly in every browser except for Chrome. I tested IE, FF, Safari, and Opera, they're all working.

My setup is that I have an iframe with buttons in it that are supposed to activate a shadowbox gallery in the parent.

My script in the main page (index.html) is:

<head>

//shadowbox linking
<link rel="stylesheet" type="text/css" href="shadowbox/shadowbox.css">
<script type="text/javascript" src="shadowbox/shadowbox.js"></script>
<script type="text/javascript">
Shadowbox.init();
</script>

//included based on what I found online to enable the click(); method to work in browsers besides IE)
<script type="text/javascript">
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); }
</script>

</head>

<body>
<a id="web1" href="http://www.google.com/" rel="shadowbox[gallery]"></a>
<a id="web2" href="http://www.yahoo.com/" rel="shadowbox[gallery]"></a>

<iframe src="frame.html">You can't see
iframes.</iframe>
</body>

My link in the iframe (frame.html) is:

<a href="#" onClick="parent.document.getElementById('web1').click();">Link1</a>
<a href="#" onClick="parent.document.getElementById('web2').click();">Link2</a>

I really hope someone can help me out =) Thanks!


Oh it seems I've found the problem -_-

There is nothing wrong with the script, it works perfectly fine. Chrome just seems to have some security issues when the files are hosted locally. I uploaded the files online and everything works fine. =)

I hope this will help someone who happens to come across it, especially linking Shadowbox from an iframe as I came across quite a few posts about it.


.click() is not supported (well) in Chrome. One way around it is to use jQuery. Convert getElementById('web2').click() to $("#web2").click().

0

精彩评论

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