Javascript 如何在新的小窗口中打开链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2088391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I cause a link to open in a new small window?
提问by user208987
I have images on a webpage that I want to have linked to another website, but in a new window of a certain size. In Dreamweaver, I used Window > Behaviors > onMouseClick, but for some reason, that isn't working. The image isn't recognized as a link.
我想将网页上的图片链接到另一个网站,但在特定大小的新窗口中。在 Dreamweaver 中,我使用了 Window > Behaviors > onMouseClick,但由于某种原因,这不起作用。该图像未被识别为链接。
Is there any other way I can have it open a link in a new window of a set size, and actually have it work this time?
有没有其他方法可以让它在设置大小的新窗口中打开一个链接,并且这次实际上让它工作?
Here is the code produced by Dreamweaver:
下面是 Dreamweaver 生成的代码:
<script language="JavaScript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}
//-->
</script>
The link:
链接:
<img src="images/portfolio/featured1.jpg" alt="Google" width="241" height="200" border="0" onclick="MM_openBrWindow('http://www.google.com','google','scrollbars=yes,width=650,height=500')" />
回答by TRiG
Well, this works for me in Opera. It's valid HTML, too.
嗯,这在 Opera 中对我有用。它也是有效的 HTML。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Test popup</title>
</head>
<body>
<script type="text/javascript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}
//-->
</script>
<p>the link:
<img src="notice.png"
alt="Google"
width="241" height="200"
style="border: 0;"
onclick="MM_openBrWindow('http://www.google.com','google','scrollbars=yes,width=650,height=500')">
</body>
</html>
And this is better:
这是更好的:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Test popup</title>
</head>
<body>
<script type="text/javascript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}
//-->
</script>
<p>the link:
<a href="http://www.google.com" onclick="MM_openBrWindow('http://www.google.com','google','scrollbars=yes,width=650,height=500'); return false;">
<img src="notice.png"
alt="Google"
width="241" height="200"
style="border: 0;"></a>
</body>
</html>
It's better because (a) there's a link, so you'll see the "hand" icon for the mouse; and (b) the link actually goes somewhere, so people with javascript turned off can still get to the content. (The "return false" on the "onclick" attribute means that people with javascript turned on only get the popup link. The "false" stops the browser following the normal link.)
更好,因为 (a) 有一个链接,所以你会看到鼠标的“手”图标;(b) 链接实际上指向某个地方,因此关闭了 javascript 的人仍然可以访问该内容。(“onclick”属性上的“return false”意味着打开 javascript 的人只能获得弹出链接。“false”会停止浏览器跟随正常链接。)

