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

Javascript Window.Open Opens the Target URL in Both a New Window and the Same

本文关键字:窗口 URL 目标 新窗口 Javascript      更新时间:2023-09-26

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

参考页面: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字符串插入窗口。打开函数,而不是使用"this.href"。当我这样做时,链接只打开一个新窗口,但不会打开相应社交媒体的共享页面,而是打开目标URL。
  • 我试图在非工作窗口后添加"返回false"。打开功能。
  • 我也试图删除"ism_fake_increment"函数只是为了测试,但再次,无济于事。
  • 我已经联系了插件作者,但他们要求访问我的网站内部,这是不会发生的。

任何想法都将非常感谢。感谢您的宝贵时间!

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

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

//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');
});
<!-- 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>