Get url of next page with javascript

Asked

Viewed 195 times

1

I am trying to compare the next page using history.go(), that is by comparing the next page with a certain url to get a positive result.

SEE:

function a() {
    if (history.go(2) == "http://www.google.com") {
        alert("ok");
    }
}
</script>
<button onclick="a()">verificar</button>

But nothing happens at all!

Another interesting thing would be if you could display the following url, with:

alert(history.go(2))

or

alert(history.forward())

But the result is undefined.

Or else check if a URL is in history:

if (window.history.go("http://www.google.com")== true){
    alert("ok");
}

Or even check if there is a following page in history, such as:

if (history.forward() == true){
    alert("ok")
}

If anyone can help me, I’d appreciate it.

  • 1

    I I suppose that this is not possible because somehow compromises the privacy of the user.

1 answer

2

The methods history.go, history.back and history.forward have no return value. The purpose of these methods is to navigate the page history, and not know what such history is.

If it were possible to read the history of the pages as you intended, this would constitute a security breach, so that malicious code could take advantage ofif that to know which pages the user accessed and then send this information to some malicious site, and therefore violating the user’s privacy.

I recommend that you do server-side tracking of the pages the user accessed on your site. Thus, you will have the relevant part of the history to your site and can redirect it as needed using history.go(url).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.