最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is it possible to get URL of previous page visited from browser using javascript - Stack Overflow

programmeradmin5浏览0评论

I am on page A.

Now I click in page A and redirected to page B.

Now I click in page B and redirected to page A.

Here i want to know the url of page B.

I have tried document.referrer but its not working.

I am on page A.

Now I click in page A and redirected to page B.

Now I click in page B and redirected to page A.

Here i want to know the url of page B.

I have tried document.referrer but its not working.

Share Improve this question edited Jun 15, 2023 at 7:56 Penny Liu 17.4k5 gold badges86 silver badges108 bronze badges asked Aug 17, 2012 at 12:15 Pradip PrajapatiPradip Prajapati 1521 gold badge5 silver badges18 bronze badges 1
  • 1 Looks like document.referrer doesn't work for navigating back. One way would be using one actual page and using history.pushState, so that you are able to keep the URLs in memory and using "different" pages at the same time. – pimvdb Commented Aug 17, 2012 at 12:20
Add a ment  | 

2 Answers 2

Reset to default 5

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.

The only way I can think of is to store the URL of the page before the user is redirected to your current page.

You could store it in a hidden form or a 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?

I took this cookie example from it to show you what I mean.

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

You can't know the URL of the previous page, but you can link to it using

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. See https://developer.mozilla/en-US/docs/DOM/Manipulating_the_browser_history

发布评论

评论列表(0)

  1. 暂无评论