使用 javascript 获取弹出窗口的当前 url

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

Get current url of the popup window with javascript

javascriptjquery

提问by Jitu

I had a webpage with a link, which opens a new page in a popup window. Everything is fine till here, The popup window contains credit card payment page held by some 3rd party server. After completing the payment flow the response is shown and there is change in the url.

我有一个带有链接的网页,可以在弹出窗口中打开一个新页面。到这里一切都很好,弹出窗口包含一些 3rd 方服务器持有的信用卡支付页面。完成支付流程后,将显示响应并且 URL 发生变化。

I need to get that url.

我需要得到那个网址。

Is it is possible in javascript?

在javascript中可以吗?

回答by hugomg

The URL of a page is accessible through the locationproperty of the window object. If you are visiting a site on the same subdomain as you , then you can get the address via

页面的 URL 可以通过window 对象的location属性访问。如果您正在访问与您位于同一子域中的站点,则可以通过以下方式获取地址

popupWindow.location.href

However, if the popup is from a third party, the same origin policyapplies and you are not allowed to inspect the location.href of the popup. The only ways to bypass the same origin policy would involve cooperation from the third party.

但是,如果弹出窗口来自第三方,则适用同源策略,并且不允许您检查弹出窗口的 location.href。绕过同源政策的唯一方法将涉及第三方的合作。

回答by Limey

Google is your friend ;D

谷歌是你的朋友;D

 var newURL = window.location.protocol + "://" + window.location.host + "/" 
              + window.location.pathname;