M. AmirQ M. AmirQ - 2 years ago 92
HTML Question

Getting string value from url

var url = "http://www.example.com/activate_account.html#123,572ad7f557455";
var userid = url.substring(url.indexOf("#") + 1);
var pass = url.substring(url.indexOf(",") + 1);

console.log("User id: " + userid + "Password: " + pass);

I would like to get string from the URL as value but when I try javascript code like above the value has been given is not what I want. The output that I get from code above is like this:

userid : 123,572ad7f557455 pass : 572ad7f557455

The problem is the userid. How can I display only 123 before the comma?

userid : 123

Answer Source

You could try to set end index for substring() method by url.indexOf(",") like this:

var userid = url.substring(url.indexOf("#") + 1, url.indexOf(","));

N.B. split() may help you if there is no , at password field.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download