I have a url like " http://localhost:8080/myapp?age=12&add=mumbai&name=myname " Now I want to add one parameter(tel=12345) as the first parameter in the query string like " http://localhost:8080/myapp?tel=12345&age=12&add=mumbai&name=myname "
I have tried below snippet
var str = "http://localhost:8080/myapp?age=12&add=mumbai&name=myname";
var txt2 = str.slice(0, str.indexOf("?")) + "tel=12345&" + str.slice(str.indexOf("?"));
alert(txt2);
But the result is incorrect
http://localhost:8080/myapptel=12345&?age=12&add=mumbai&name=myname
Is there a better way???
Try this:
var str = "http://localhost:8080/myapp?age=12&add=mumbai&name=myname"; var txt2 = str.slice(0, str.indexOf("?")) + "?" + "tel=12345&" // ^^^^^ + str.slice(str.indexOf("?") + 1); // ^^^ alert(txt2);
You need to just increment index by 1 and this will work.
Eg:
var txt2 = str.slice(0, str.indexOf("?") + 1 ) + "tel=12345&" + str.slice(str.indexOf("?") + 1);
var txt2=str.split('?')[0]+'?tel=12345&'+str.split('?')[1];
只是一个变化。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.