获取上一页 URL,打开新窗口,然后通过 Javascript 重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13401174/
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
Get previous page URL, Open New Window, then Redirect through Javascript
提问by RCNeil
I'm stuck using a CMS that only gives me the ability to modify the content of the <body>
, so when I want to redirect people, I've used this
我坚持使用CMS只给我修改的内容的能力<body>
,所以当我想重定向的人,我用这个
<script type="text/javascript">
window.location = "http://www.example.com/"
</script>
So, yes, the page loads first, and then 5ms later, the redirect happens, and it's worked for all intensive purposes. Now, I'm wondering if I can use javascript to do something else.
所以,是的,页面首先加载,然后 5 毫秒后,重定向发生,并且它适用于所有密集目的。现在,我想知道是否可以使用 javascript 来做其他事情。
Is it possible to open a new browser tab, with a specified URL, and then redirect the user back to the previous page, through Javascript?
是否可以使用指定的 URL 打开一个新的浏览器选项卡,然后通过 Javascript 将用户重定向回上一页?
Many thanks, SO.
非常感谢,所以。
EDIT - Whether it opens a new window or tab, to be honest, is not as important as it actually functioning. I need Javascript to determine the prior page (if possible), then open a new window/tab to a URL I specify, and then redirect the current window/tab to it's prior page. Some are saying that window.open
only works on a click event, which will not work for what I am trying accomplish either... just fyi.
编辑 - 老实说,它是否打开一个新窗口或选项卡并不像它实际运行那么重要。我需要 Javascript 来确定前一页(如果可能),然后打开一个新窗口/标签到我指定的 URL,然后将当前窗口/标签重定向到它的前一页。有人说window.open
这只适用于点击事件,这对我正在尝试完成的事情也不起作用......仅供参考。
So, literally, without clicks, I need Javascript to do the following -
所以,从字面上看,没有点击,我需要 Javascript 来执行以下操作 -
- Determine the prior/previous/last page the user came from, store it as a variable
- Open a new window or tab, to a specified URL
window.location
back to the prior page, which I stored as a variable
- 确定用户来自的上一页/上一页/最后一页,将其存储为变量
- 打开一个新的窗口或选项卡,到指定的 URL
window.location
返回上一页,我将其存储为变量
Hope that makes sense.
希望这是有道理的。
回答by Sean Airey
Depending on the user's browser setting using window.open
can open the new window in a new tab instead but you CANNOT directly control this through the browser. It is all down to the user's settings.
根据用户的浏览器设置,使用window.open
可以在新选项卡中打开新窗口,但您不能直接通过浏览器进行控制。这完全取决于用户的设置。
To open a new window:
要打开一个新窗口:
window.open("http://www.google.com", "windowName", "window options (optional parameter)");
Then simply use:
然后简单地使用:
history.back();
You can also use the referer property:
您还可以使用引用属性:
var previousUrl = document.referrer;
For more info on window.open
, see: http://www.javascript-coder.com/window-popup/javascript-window-open.phtml
有关 的更多信息window.open
,请参阅:http: //www.javascript-coder.com/window-popup/javascript-window-open.phtml
For more info on the document.referrer property, take a look at: http://www.netmechanic.com/news/vol4/javascript_no14.htm
有关 document.referrer 属性的更多信息,请查看:http: //www.netmechanic.com/news/vol4/javascript_no14.htm