Reading URL Parameters Using JavaScript
Some sites require to read URL (Uniform Resource Locator) parameter values , that can use current location.
Passing client submitted values can transfer to one page to another page have number of ways. One of the best way to transfer values from one page to another page is just attach value to URL parameters.
For example
https://bestvwant.org/birthday-wishes.html?a=siva&b=ram&c=20
In the above example, ?a=siva&b=ram&c=2
Question mark (?) starts on words parameter beginning, each parameter separate to ampersent (&) symbol .
Read URL parameters at Client Slide
Best method to read URL parameters at Client Slide using JavaScript code. For blogger website users also use JavaScript code to read parameters. No support of php, asp and more server side scripting languages.
JavaScript code to read URL Parameters
<script>
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
var x = getUrlVars()["a"];
var y = getUrlVars()["b"];
var z = getUrlVars()["c"];
</script>
In the above code the window.location method to read url parameters. Display URL parameter value in current page
<script> document.writeln(x); document.writeln(y); document.writeln(z); </script>
Replace or eliminate URL decoder parameters
For example
See parameter 'a' contains 2 words (siva+raju) , combining with pluse (+), to display parameter a value using document.writeln(a), directly display as siva+raju.
So to eliminate '+' symbol use following method
var x1 = x.replace('+', ' ');
To eliminate continuous of '+' symbols like (siva+rama+raju) use following method.
var x1 = x.replace(/\+/g, ' ');
If URL Parameters contains decoder values , to eliminate that values using JavaScript method as
var x1=decodeURIComponent(x);
Note: The JavaScript function encodes space as %20. More info see
Now see complete example code here
<script>
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
var wname = getUrlVars()["wname"];
var wname = wname.replace(/\+/g, ' ');
var yname = getUrlVars()["yname"];
var yname = yname.replace(/\+/g, ' ');
var m1 = getUrlVars()["m1"];
var m1 = decodeURIComponent(m1);
var m11 = m1.replace(/\+/g, ' ');
var m2 = getUrlVars()["m2"];
var m2 = decodeURIComponent(m2);
var m22 = m2.replace(/\+/g, ' ');
</script>
<script>document.writeln(wname);</script>
see this
Informative stuff. Actually, I'm also a student and find out the solved or answers to my syllabus questions & landed here. From time to time in fictionary books of lineserved.com, there are unpleasant elements that meet this criterion. They can talk about phenomena that are questionable from an ethical point of view, give us an idea of the inner side of evil. Its a fact you have also shared an enjoyable and informative piece of content here. Anyways, thanks for sharing the nice piece of stuff with us.
ReplyDelete