user379888
user379888

Reputation:

Creating link with javascript

I am trying to create a link in JS that moves the person the page from where he has come from. Here is the code.

<script  language="javascript">
function Jump()
{
document.href=document.referrer;
}
</script>

Here is the html,

<a href="#" onclick="Jump();">Skip and Continue</a>

Now when the user clicks on the link, nothing happens. Please guide me where I am doing wrong. Thanks

Upvotes: 0

Views: 90

Answers (4)

kennebec
kennebec

Reputation: 104840

It is not document.href but window.location.href...

<script>
function Jump(){
    if(document.referrer)location.href=document.referrer;
    else history.back();
}
</script>

(If you used the jump to get to the current page, there is no referrer.)

Upvotes: 0

tckmn
tckmn

Reputation: 59343

It is better to bind a click listener than use onclick.

Try changing it to this:

<a id="myLink" href="#">Skip and Continue</a>

And Javascript:

<script type="text/javascript">
document.getElementById('myLink').addEventListener('click', function(e) {
    e.preventDefault();
    document.location.href=document.referrer; //actually better as "history.back()"
}
</script>

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324790

Many browsers will not use document.referrer for privacy reasons, especially if the referrer is from another domain.

Instead, try onclick="history.go(-1)" instead of your Jump() function.

Upvotes: 0

grepit
grepit

Reputation: 22392

how about using the below code to move back

 history.back();

Upvotes: 1

Related Questions