简体   繁体   English

是否可以使用javascript从浏览器获取上一页的URL

[英]Is it possible to get URL of previous page visited from browser using javascript

I am on page A. 我在A页上。

Now I click in page A and redirected to page B. Now I click in page B and redirected to page A. 现在,我单击页面A并重定向到页面B。现在我单击页面B并重定向到页面A。

Here i want to know the url of page B. 在这里我想知道页面B的网址。

I have tried document.referrer but its not working. 我尝试了document.referrer,但无法正常工作。

document.referrer works when a user clicks on a link to navigate to the current page but I don't think it works when you do a manual redirect in a Javascript function. 当用户单击链接导航到当前页面时,document.referrer起作用,但是当您在Javascript函数中进行手动重定向时,我认为它不起作用。

The only way I can think of is to store the URL of the page before the user is redirected to your current page. 我能想到的唯一方法是在用户重定向到当前页面之前存储页面的URL。

You could store it in a hidden form or a cookie. 您可以将其存储为隐藏形式或cookie。

This question has also been asked before and its worth having a quick read through here. 之前也曾问过这个问题,值得在这里快速阅读。 How do you get the previous url in Javascript? 您如何获得Javascript中的先前网址?

I took this cookie example from it to show you what I mean. 我从中获得了这个cookie示例,向您展示了我的意思。

$.cookie("previousUrl", window.location.href, {path:"/"});

You can't know the URL of the previous page, but you can link to it using 您不知道上一页的URL,但是可以使用链接到它

window.history.back();

or 要么

window.history.go(-1);

if you want to know if there is a previous page, check 如果您想知道是否有上一页,请检查

window.history.length;

If it's more than 0, there is a prev page. 如果大于0,则存在上一页。 See https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history 参见https://developer.mozilla.org/zh-CN/docs/DOM/Manipulating_the_browser_history

You can use history object of javascript. 您可以使用javascript的历史记录对象。 Please refer the url http://www.w3schools.com/jsref/obj_history.asp 请参考网址http://www.w3schools.com/jsref/obj_history.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM