0

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

2
  • 3
    try this: document.location.href=document.referrer; Commented Feb 18, 2013 at 0:34
  • maybe your referrer is empty Commented Feb 18, 2013 at 0:36

4 Answers 4

1

how about using the below code to move back

 history.back();
Sign up to request clarification or add additional context in comments.

1 Comment

add this code to see if the broswer you are using support referrer. if( 'referrer' in document ) { console.log(document.referrer); }
0

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.

Comments

0

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>

2 Comments

document.href isn't a thing.
@sachleen Oh oops, copy/pasted. Edited.
0

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.)

Comments

Your Answer

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