[英]How to open a link in a new window and also in a new tab/window with right click?
我有一個幫助鏈接。 如果用戶單擊它,它將打開一個具有固定寬度和高度的新窗口。 效果很好,除了當我右鍵單擊鏈接時,沒有選項“在新標簽頁中打開”(在IE中),或者我可以在新標簽頁中打開,但被定向到空白頁(chrome)。 有誰能像鏈接一樣幫助您實現此目的,並且默認情況下也可以在新窗口(不是標簽頁)中打開它嗎?
<html>
<head>
<title>
link
</title>
<script type="text/javascript">
function activateHelpView(helpUri) {
var WindowId = 'SomeWindowId';
var helpWindow = window.open(helpUri, WindowId, 'width=400,height=500,menubar=no,status=no,scrollbars=no,titlebar=no,toolbar=no,resizable=yes');
if (helpWindow) {
(helpWindow).focus();
}
}
</script>
</head>
<body>
<a id='PortOrderPageLearnMoreLink' href='javascript:' title='Learn more' onclick='activateHelpView("http://stackoverflow.com/")'>Learn more</a>
</body>
</html>
使用真實鏈接,而不是空的javascript:
地址。 onclick
處理程序可以阻止鏈接執行任何“正常”操作,但是右鍵單擊將具有一些作用。
target=_blank
是一個很強的暗示,您希望在新窗口中打開頁面,但是,是否完全尊重(無論是在窗口中還是在選項卡中)頁面都是無法控制的。
<script type="text/javascript"> function activateHelpView(helpUri) { var WindowId = 'SomeWindowId'; var helpWindow = window.open(helpUri, WindowId, 'width=400,height=500,menubar=no,status=no,scrollbars=no,titlebar=no,toolbar=no,resizable=yes'); if (helpWindow) { (helpWindow).focus(); } } </script> <a id='PortOrderPageLearnMoreLink' href='http://stackoverflow.com/' title='Learn more' onclick='activateHelpView(this.href); return false;' target='_blank'>Learn more</a>
處理所有這些問題的一種更現代的方法(尤其是在有多個幫助鏈接的情況下)是為所有這些方法添加一個類,然后運行一些JavaScript依次向其添加點擊處理程序。 HTML保持干凈(並帶有真實鏈接,如果禁用或未加載JavaScript,則HTML仍然有效)。
var helplinks = document.querySelectorAll('.helplink'); for (var i = 0; i < helplinks.length; ++i) { helplinks[i].addEventListener('click', activateHelpView); } function activateHelpView(event) { event.stopPropagation(); // don't let the click run its course event.preventDefault(); var helpUri = this.href; // "this" will be the link that was clicked var WindowId = 'SomeWindowId'; var helpWindow = window.open(helpUri, WindowId, 'width=400,height=500,menubar=no,status=no,scrollbars=no,titlebar=no,toolbar=no,resizable=yes'); if (helpWindow) { helpWindow.focus(); } }
<a id='PortOrderPageLearnMoreLink' href='http://stackoverflow.com/' title='Learn more' class='helplink' target='_blank'>Learn more</a>
不允許StackOverflow片段使用其中某些功能。 一個有效的例子可以在這里找到。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.