0

I would like to change the url of the page when the user select another page to visit. The url is dynamically replace the original one.

eg.

If user visit page 1 , the url will be book.html?page=1

If page 30 then book.html?page=30 and so on.

However, when I change the link using javascript, it falls into a infinite loop.

It seems I keep visit->change link ->visit ->change link->.... How to fix this problem?

eg. When the link change, don't access the page.

var currURL = $(location).attr('href');
var index = currURL.indexOf('?');
currURL = currURL.substring(0, index != -1 ? index : currURL.length);
// fall into loop
$(location).attr('href', currURL + '?page=' + pageNo); 
2
  • Issues in your ternary operator I think, what are the values you get in currURL.length Commented Dec 27, 2012 at 4:46
  • 3
    This is because when you change location.href it will reload the whole page and run your code again? Commented Dec 27, 2012 at 4:56

1 Answer 1

1

You can do this pretty easily with just standard javascript.

if(location.href.indexOf('?') !== -1 && location.href.indexof('?page=') === -1)
{
    var urlArray = location.href.split('?');
    var newURL = urlArray[0] + "?page=" + urlArray[1];
    location.href = newURL;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.