Home > Net >  Can't redirect to url by javascript function
Can't redirect to url by javascript function

Time:01-15

I need a button, which lead up in url path like this:

from /host/elements/1 to /host/elements

I have a link in my html:

<a href="javascript:go_backward()"> </a>

And a javascript function:

function go_backward() {
    var url = window.location.href;
    var page = url.substring(0, url.lastIndexOf('/'))
    window.location.assign(page);
    return false;
}

But when i click the button i only get desired url printed, without correct redirect. Why cat it be so?

CodePudding user response:

Change:

window.location.assign(page);
return false;

to

window.location.href = page;

If you just want to go back you can also use:

window.history.go(-1)
  •  Tags:  
  • Related