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?

Kunj
  • 1,980
  • 2
  • 22
  • 34
raspi surya
  • 109
  • 4
  • 15

6 Answers6

1
 alert(document.referrer);

That should alert the referrer.

Kento Nishi
  • 578
  • 1
  • 9
  • 24
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.

Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
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);
Albert Einstein
  • 7,472
  • 8
  • 36
  • 71
  • 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 – raspi surya Apr 05 '18 at 06:16
  • It's blank because there's no origin. Put in the to get an origin for a locally-opened html file. Even better, install a localhost and run it on there. – Victor Stoddard Apr 07 '18 at 23:10
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'))
rozalex
  • 320
  • 1
  • 8
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>
Victor Stoddard
  • 3,582
  • 2
  • 27
  • 27
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>