0

Title says it all. Basically I am looking to retrieve the end of the url using jQuery to add one to the selection and go to that page. I had some help from someone else on her but I want to understand how it is working/ why it's not working:

var urlFrags = window.location.href.replace(".html", "").split("_"),

curPage = parseInt(urlFrags[urlFrags.length]),

nextPage = "example_" + (curPage + 1) + ".html";

It could be an error somewhere else, but I am working down the line trying to bug-fix it. I understand pretty much everything going on, except I don't understand parseInt() and stuff.

Would it be possible to '.getAsIntiger(urlFrags)' ? (I have tried this but I may have implemented wrong.)

2
  • parseInt() will turn a String into an Integer. If for example you have the string "Text1"*, it will turn into 1. With that 1 you can perform mathematical equetions. *it strips out all the text that cannot be turned into a number. Parameters and more at developer.mozilla.org/de/docs/Web/JavaScript/Reference/… Commented Sep 3, 2014 at 14:59
  • If your format is same then you could also use replace method to get your desired output as below: "example_" + (parseInt("example_3.html".replace("example_","").replace(".html","")) + 1) + ".html" Commented Sep 3, 2014 at 15:05

2 Answers 2

1

The error there is here:

curPage = parseInt(urlFrags[urlFrags.length]);

You should be doing

curPage = parseInt(urlFrags[urlFrags.length-1]);

Since if you have a length of 2 in your array, you cannot access Array[2] because it doesn't exist. You have to access Array[1] because the array index starts in 0.

Sign up to request clarification or add additional context in comments.

1 Comment

YOU SIR AND OR MADAM HAVE MADE MY WEEK!
1

Why not do a var num = parseInt(/.+?_(\d+)\.html/.exec(window.location.href)[1])

1 Comment

Personally I find the OP's solution much easier on the eyes, and I can read Regex just fine. Additionally you are assuming only numbers 0-9 and not double/triple digits.

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.