How to get the previous URL in JavaScript?

后端 未结 7 775
野趣味
野趣味 2020-11-22 06:05

Is there any way to get the previous URL in JavaScript? Something like this:

alert(\"previous url is: \" + window.history.previous.href);

7条回答
  •  长情又很酷
    2020-11-22 06:24

    document.referrer is not the same as the actual URL in all situations.

    I have an application where I need to establish a frameset with 2 frames. One frame is known, the other is the page I am linking from. It would seem that document.referrer would be ideal because you would not have to pass the actual file name to the frameset document.

    However, if you later change the bottom frame page and then use history.back() it does not load the original page into the bottom frame, instead it reloads document.referrer and as a result the frameset is gone and you are back to the original starting window.

    Took me a little while to understand this. So in the history array, document.referrer is not only a URL, it is apparently the referrer window specification as well. At least, that is the best way I can understand it at this time.

提交回复
热议问题