I need to call a javascript function and pass a long url in that. which gives unescaped error because of some special characters. i can't use escape characters because the url is picked dynamically and passed to the function. How can i do this?
Asked
Active
Viewed 2.2k times
2 Answers
0
I had the need to read a URL GET variable and complete an action based on the url parameter. I searched high and low for a solution and came across this little piece of code on Snipplr. It basically reads the current page url, perform some regular expression on the URL then saves the url parameters in an associative array, which we can easily access.So as an example if we had the following url with the javascript at the bottom in place.
http://papermashup.com/index.php?id=123&page=home
All we’d need to do to get the parameters id and page are to call this:
var first = getUrlVars()["id"];
var second = getUrlVars()["page"];
alert(first);
alert(second);
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}

AlexB
- 7,302
- 12
- 56
- 74

Jayaraman M
- 21
- 3