Javascript Window.Open 在新的 Window 和相同的 URL 中打开目标 URL
Javascript Window.Open Opens the Target URL in Both a New Window and the Same
基本上,我有一个带有社交媒体分享按钮的页面。其中一些可以正常工作(它们在新 window 中打开),但是,其他一些在新 window 和同一个 window 中打开。我已经为此疯狂了一天,我似乎找不到解决它的方法。
Link 按预期工作(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/');">
Link 打开 URL 以共享新的 window 和相同的:
<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/&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"。当我这样做时,link 只打开一个新的 window,但不会打开相应社交媒体的共享页面,而是打开目标 URL.
- 我尝试在无效的 window.open 函数后添加 "return false"。
- 我也曾尝试删除 "ism_fake_increment" 功能只是为了测试,但还是无济于事。
- 我已经联系了插件作者,但他们要求在内部访问我的网站,这不会发生。
任何想法将不胜感激。感谢您的宝贵时间!
我建议您不要使用 onclick
属性,因为它会导致代码极其混乱。相反,在 DOM.
中使用 .addEventListener()
要禁用 link 在同一个 window 中打开 link,只需禁用默认设置即可。这可以通过调用传递到回调中的对象的 .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/&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>
基本上,我有一个带有社交媒体分享按钮的页面。其中一些可以正常工作(它们在新 window 中打开),但是,其他一些在新 window 和同一个 window 中打开。我已经为此疯狂了一天,我似乎找不到解决它的方法。
Link 按预期工作(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/');">
Link 打开 URL 以共享新的 window 和相同的:
<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/&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"。当我这样做时,link 只打开一个新的 window,但不会打开相应社交媒体的共享页面,而是打开目标 URL.
- 我尝试在无效的 window.open 函数后添加 "return false"。
- 我也曾尝试删除 "ism_fake_increment" 功能只是为了测试,但还是无济于事。
- 我已经联系了插件作者,但他们要求在内部访问我的网站,这不会发生。
任何想法将不胜感激。感谢您的宝贵时间!
我建议您不要使用 onclick
属性,因为它会导致代码极其混乱。相反,在 DOM.
.addEventListener()
要禁用 link 在同一个 window 中打开 link,只需禁用默认设置即可。这可以通过调用传递到回调中的对象的 .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/&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>