如何在不切换到新选项卡的情况下使用javascript打开新选项卡?
例如,当用户单击某个链接时,将打开一个新选项卡,但用户应该保留在当前选项卡上。如何在不切换到新选项卡的情况下在JavaScript中打开新选项卡?
回答
Web浏览器会自动聚焦新选项卡上,但你可以叫焦点回到:
function openWindow(url)
{
window.open(url, '_blank');
window.focus();
}
<a href="http://www.example.com/" onclick="javascript:openWindow(this.href);return false;">Click Me</a>
不幸的是,你目前无法做到这一点 - 但你可以关闭。您可以打开一个新的窗口,如果您没有指定任何窗口尺寸或窗口功能,那么大多数现代浏览器都会打开一个新选项卡(取决于用户的偏好,但随后您希望执行用户喜欢的操作无论如何吧?)。所以只需要window.open(url)
或window.open(url, name)
,如果你打算使用某个名字的话。一定要直接回应用户发起的事件,否则浏览器的弹出式窗口拦截器可能会...阻止弹出窗口。 :-)
关于保持专注在你窗上...祝你好运。您可以拨window.focus()
后window.open(...)
,但根据我的经验,这通常不起作用。
把它扔在那里:如果您的用户与URL的真正联系相互作用的东西,用户可以决定是否要在新标签中打开一个新的窗口,无论是否给它焦点(如果它们足够复杂,可以知道Shift + Click和Ctrl + Shift + Click或右键单击菜单)。
这是用户特定的设置,你不能从JS改变这种行为。
不幸的是,你不能在所有的浏览器做到这一点,但如果你能做到这一点在Chrome实现浏览器的扩展。 如何通过JavaScript和标签操作在Chrome:
http://code.google.com/chrome/extensions/tabs.html
chrome.tabs.create(object createProperties, function callback)
Creates a new tab. Note: This function can be used without requesting the 'tabs' permission in the manifest.
Parameters
**createProperties** (object)
**windowId** (optional integer)
The window to create the new tab in. Defaults to the current window.
**index** (optional integer)
The position the tab should take in the window. The provided value will be clamped to between zero and the number of tabs in the window.
**url** (optional string)
The URL to navigate the tab to initially. Fully-qualified URLs must include a scheme (i.e. 'http://www.google.com', not 'www.google.com'). Relative URLs will be relative to the current page within the extension. Defaults to the New Tab Page.
**selected** (optional boolean)
Whether the tab should become the selected tab in the window. Defaults to true
pinned (optional boolean)
Whether the tab should be pinned. Defaults to false
**callback** (optional function)
(function(a){
document.body.appendChild(a);
a.setAttribute('href', location.href);
a.dispatchEvent((function(e){
e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, false, 0, null);
return e
}(document.createEvent('MouseEvents'))))}(document.createElement('a')))
邮政是旧的,但没有任何100%正确的答案。
解所有的浏览器:
你可以打开新标签页中同一域中通过网址:
window.open("newurl.php", "_blank");
和跨域新的标签,你必须创建一个简单的本地脚本,如这样的:
在新文件“redirect.php”你传递一个参数(目标新标签的URL)这样
<?php
$newURL=$_GET['u'];
header('Location: '.$newURL);
؟>
,你可以与任何目标URL创建一个新的标签页中:
window.open("redirect.php?u=http://www.google.com", "_blank");
:)
的我在我的网站的解决方案。与任何
工作对我来说,使用
window.open("https://stackoverflow.com");
完美。这取决于您使用的浏览器,操作系统和个人偏好。
Chrome,Internet Explorer和Firefox都打开了一个新选项卡。对不起,如果这不适合你。
(就意识到,这是一个古老的线程)
- 1. 如何打开新选项卡时打开新选项卡
- 2. jQuery UI选项卡:如何在不重新加载的情况下切换到(ajax)选项卡?
- 3. 在新选项卡中打开链接,只有新选项卡
- 4. 如何切换到新选项卡
- 5. 如何使用Excel在打开的Chrome选项卡中打开新URL而不打开新选项卡
- 6. Codeception + PhantomJS:如何打开新选项卡并切换到它
- 7. 如何防止Windows.Forms.TabControl在某些情况下切换选项卡?
- 8. 如何在Atom的新选项卡中打开新项目?
- 9. MacVim + Command-T插件,默认情况下在新选项卡中如何打开
- 10. Xrm.Utility.openwebresource打开新选项卡
- 11. 在新选项卡中打开延迟
- 12. 在新选项卡中打开Base64
- 13. 在新选项卡中打开网站
- 14. 在新选项卡中打开PDF
- 15. 在新选项卡中打开链接
- 16. 在新选项卡中打开
- 17. 在RSelenium中打开新选项卡
- 18. 在新选项卡中打开图片
- 19. 在新选项卡中打开页面
- 20. AngularJS在新选项卡中打开PDF
- 21. 在新选项卡中打开
- 22. 在新选项卡中打开PDF C#
- 23. 在新选项卡中打开url
- 24. GVim:在新选项卡中打开
- 25. window.open在函数中替换当前选项卡以及打开新选项卡
- 26. 选择ActionBar选项卡时打开新行选项卡
- 27. 如何从BHO打开新选项卡?
- 28. 如何在新选项卡中打开div链接Javascript onClick =“window.location
- 29. Selenium在SendKeys后打开新选项卡
- 30. 使用javascript打开新选项卡,但保留在当前选项卡上
重新聚焦:根据我的经验没有。无论如何(http://jsbin.com/aboluk/2)不是Chrome,但它们在尝试中没有多大的伤害。 –
@ T.J.Crowder:这对我来说在Chrome 15中确实有效。 – josh3736
@ josh3736:这很有趣。我仍然在14(在Linux上),但它没有。所以基本上:你不能依靠它。 :) –