Javascript Window.Open在新窗口和同一窗口中打开目标URL

时间:2015-08-23 14:49:45

标签: javascript wordpress wordpress-plugin window.open

基本上,我有一个带有社交媒体共享按钮的页面。其中一些工作正常(它们在新窗口中打开),然而,其他人在新窗口和同一窗口中打开。我现在已经疯了一天了,我似乎无法找到解决方法。

供参考,这是页面:http://www.inetsolutions.org/gsa-search-engine-ranker-ultimate-tutorial-and-genuine-review-seo-software-of-the-gods/

链接按预期工作(Facebook,Twitter,Pinterest):

<a href="javascript:void(0)" class="ism_link" onclick="indeedPinterestPopUp(2513);ism_fake_increment('.pinterest_share_count', 'pinterest', 'http://www.inetsolutions.org/gsa-search-engine-ranker-ultimate-tutorial-and-genuine-review-seo-software-of-the-gods/');">

打开要在新窗口和相同位置共享的URL的链接:

<a href="http://www.stumbleupon.com/badge/?url=http://www.inetsolutions.org/gsa-search-engine-ranker-ultimate-tutorial-and-genuine-review-seo-software-of-the-gods/&amp;title=GSA%20Search%20Engine%20Ranker%20Ultimate%20Tutorial%20and%20Genuine%20Review%20%E2%80%93%20SEO%20Software%20of%20the%20Gods" class="ism_link" onclick="ism_fake_increment('.stumbleupon_share_count', 'stumbleupon', 'http://www.inetsolutions.org/gsa-search-engine-ranker-ultimate-tutorial-and-genuine-review-seo-software-of-the-gods/');return !window.open(this.href, '', 'width=700,height=575');">

我尝试过:

  • 我尝试删除标记的“href”属性,并将URL字符串插入window.open函数,而不是使用“this.href”。当我这样做时,该链接只打开一个新窗口,但不会打开相应社交媒体的共享页面,而是打开目标URL。
  • 我试图在非工作window.open函数之后添加“return false”。
  • 我还试图删除“ism_fake_increment”函数只是为了测试,但是再次无效。
  • 我已经联系了插件作者,但是他们要求在内部访问我的网站,这不会发生。

任何想法都将受到强烈赞赏。谢谢你的时间!

1 个答案:

答案 0 :(得分:2)

我建议您不要使用onclick属性,因为它会导致代码非常混乱。相反,请使用DOM中的.addEventListener()

要禁用链接在同一窗口中打开链接,只需禁用默认值即可。这可以通过调用传递给回调的对象的.addEventListener()方法在回调中使用.preventDefault()来完成:

&#13;
&#13;
//Get our link:
var link = document.getElementById("stumbleupon");
//Bind the click event:
link.addEventListener("click", function(event) {
    //Prevent the link from opening regularly with .preventDefault():
    event.preventDefault();
    //The following code with the plugin does not work because we haven't included the plugin in the code snippet, but as you can clearly see if you click the link, the link has clearly been disabled because of the above call to .preventDefault().
     
    //Do different stuff with the plugin:
    ism_fake_increment('.stumbleupon_share_count', 'stumbleupon', 'http://www.inetsolutions.org/gsa-search-engine-ranker-ultimate-tutorial-and-genuine-review-seo-software-of-the-gods/');
    return !window.open(this.href, '', 'width=700,height=575');
});
&#13;
<!-- Set the ID attribute so we can find this link in the DOM: -->
<a id="stumbleupon" href="http://www.stumbleupon.com/badge/?url=http://www.inetsolutions.org/gsa-search-engine-ranker-ultimate-tutorial-and-genuine-review-seo-software-of-the-gods/&amp;title=GSA%20Search%20Engine%20Ranker%20Ultimate%20Tutorial%20and%20Genuine%20Review%20%E2%80%93%20SEO%20Software%20of%20the%20Gods" class="ism_link">Hello! This is a link to stumbleupon.com!</a>
&#13;
&#13;
&#13;