如何使用 jQuery 打开一个新窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8075948/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 09:57:58  来源:igfitidea点击:

How do I open a new window using jQuery?

jquery

提问by Samantha J T Star

I have the following two ways suggested to me.

我向我建议了以下两种方法。

window.location.href = '/Administration/Notes/Create?dsValue=a&selectAnswer=b';
$.get("/Administration/Notes/Create", { dsValue: dsValue, selectedAnswer: answer });

Are these methods the same? Which one would be the best for me to use and what's the difference between the two?

这些方法一样吗?哪一个最适合我使用,两者之间有什么区别?

回答by Feisty Mango

It's not really something you need jQuery to do. There is a very simple plain old javascript method for doing this:

这真的不是你需要 jQuery 做的事情。有一个非常简单的普通旧 javascript 方法来执行此操作:

window.open('http://www.google.com','GoogleWindow', 'width=800, height=600');

That's it.

就是这样。

The first arg is the url, the second is the name of the window, this should be specified because IE will throw a fit about trying to use window.openerlater if there was no window name specified (just a little FYI), and the last two params are width/height.

第一个 arg 是 url,第二个是窗口的名称,这应该被指定,因为window.opener如果没有指定窗口名称(仅供参考),IE 会在稍后尝试使用时引发不适,最后两个参数是宽度/高度。

EDIT: Full specification can be found in the link mmmshuddup provided.

编辑:完整规范可以在提供的链接 mmmshuddup 中找到。

回答by Yes Barry

Those are by no means the same. The first will simply send you to whatever URL you have assigned to window.location.href (in the same window you're currently in). The second makes a GET AJAX request.

这些绝不相同。第一个将简单地将您发送到您分配给 window.location.href 的任何 URL(在您当前所在的同一窗口中)。第二个发出 GET AJAX 请求。

Try this page: http://www.codebelt.com/jquery/open-new-browser-window-with-jquery-custom-size/

试试这个页面:http: //www.codebelt.com/jquery/open-new-browser-window-with-jquery-custom-size/

It gives a great example on how to open a new window*.

它提供了一个关于如何打开新窗口的很好的例子*。

If you wish to use raw javascript then this is what you're looking for:

如果您想使用原始 javascript,那么这就是您要寻找的:

window.open(URL,name,specs,replace)

As seen in http://www.w3schools.com/jsref/met_win_open.asp

http://www.w3schools.com/jsref/met_win_open.asp 所示

回答by chuckfinley

This works:

这有效:

myWindow = window.open('http://www.yahoo.com','myWindow', "width=200, height=200");