是否可以使用javascript从另一个网站域获取上一个网站页面的url,例如:
用户在浏览器中有此网站网址:
www.example.com/page1.html
然后用户清除了浏览器地址栏并编写了我的网站
网址:www.test.com
我需要的是使用 javascript 来获取
www.example.com/page1.html 网址。
这可能吗?
最佳答案
不,出于同样的原因,如果 iframe 位于不同的域中,则无法从 iframe 获取 url:跨域安全性。
你可以做的是在 php 中获取 $_SERVER['HTTP_REFERER']
但它并不总是被设置...
或者正如 @FabrícioMatté 在 javascript 中评论 document.referrer
一样,但可靠性同样糟糕。
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.
http://php.net/manual/en/reserved.variables.server.php
要返回上一页,您可以在 JavaScript 中使用 history.go(-1)
。
关于javascript - 如何使用javascript获取上一页URL,不同的域名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25094699/