0

I have page A and B. in my page A, I have link that direct me to page B. What I want to do is, when I click the link in page A, it will open page B, and also give me alert of page A`s URL. Here is what I try:

page A:

<html>
<head>
<a href="file:///C:/Users/jason/Desktop/pageb.html">click me</a> 
</head>
</html>

page B:

<html>
<head>
<script type="text/javascript">

var oldURL = document.referrer;
alert(oldURL);

</script>
</head>

It opens the page B when I click it, but the alert is blank. How can i do this?

6
  • 1
    Move the <a> tag out of the <head> element and place it into a <body> element. Commented Apr 5, 2018 at 4:50
  • I am sorry, i edit the question.. I alert the oldURL now. But it is not working Commented Apr 5, 2018 at 4:58
  • @raspisurya - It works fine as it is with just some minor changes. You don't need to do window.open() or use local storage or anything crazy like that. The original issue with the code has to do with the file:/// URI you've used for page B. There will be no referrer. But if you run this code from a server (with the <a> in the correct place) it works fine. Here's proof: plnkr.co/edit/WMaE76VndKpQRGQ01iTz?p=preview Commented Apr 5, 2018 at 5:05
  • i don't understand why it give me the blank result in my local? what is the difference Commented Apr 5, 2018 at 6:11
  • Rather than explain this here, I put the explanation into an answer. It accurately and correctly addresses your question. The other answers do not address the real issue. I would appreciate if you would accept my answer. Thanks. Commented Apr 5, 2018 at 14:17

6 Answers 6

1
 alert(document.referrer);

That should alert the referrer.

Sign up to request clarification or add additional context in comments.

Comments

1

NOTE: I moved this out of the comments because it describes the real issue.

The original issue with the code has to do with the file:/// URI you've used for page B. There will be no referrer. But if you run this code from a server it works fine. Here's proof: Plunkr

When you load a file from the file system (using file:// protocol) the referrer header is not set. You can easily see the difference if you open the network panel in dev tools and look at the headers for both pages. Opened with file:/// no headers, from Plunkr has correct referrer header.

So your code works fine (but the anchor tag should be moved).

You don't need to do window.open() or use local storage or anything crazy like that.

Comments

0

You can save your latest visited url in sessionStorage on Page A:

sessionStorage.setItem('url', window.location.href);

on Page B

alert(sessionStorage.getItem('url'))

1 Comment

what if I don't have any access to edit the source page A? what I can do is only in page B
0

Add <a href="file:///C:/Users/jason/Desktop/pageb.html">click me</a> out of the head tag. And print oldURL variable insted of window.location.href which is the current URL.

Page A

<html>
<head>

</head>

<a href="file:///C:/Users/jason/Desktop/pageb.html">click me</a> 

</html>

Page B

var oldURL = document.referrer;
alert(oldURL);

2 Comments

I have tried this but this is not working in my local PC. still blank alert comes out. I put the pagea.html and pageb.html in my desktop
It's blank because there's no origin. Put <meta name="referrer" content="origin"> in the <head> to get an origin for a locally-opened html file. Even better, install a localhost and run it on there.
0

You can pass it in the URL:

 window.open("b.html?referer=" + window.location.href);

You can also use the opener object to get a parent window's variable:

a.html

<html>
 <script>
   var referrer = window.location.href;
   var win = window.open("b.html");
 </script>
</html>

b.html

<html>
 <script>
   alert(window.opener.referrer);
 </script>
</html>

As @RandyCasburn mentioned above, you also have to move the tag from the head to the body for it to be visible:

<html>
<head>
</head>
<body>
    <a href="file:///C:/Users/jason/Desktop/pageb.html">click me</a> 
</body>
</html>

1 Comment

With the <a> in the correct place, the use of window.open() is unnecessary as a solution because the anchor tag will accomplish the goal without code.
0

You can use document.referrer

Refer the following code snippet

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Alert</title>
</head>

<body>
    <a href="https://www.google.co.in/" onclick="onLinkClick()">Link</a>
    <script>

        function onLinkClick() {
            alert("Redirected from==>"+document.referrer);

        }
    </script>
</body>

</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.