Last updated on January 23rd, 2023 at 01:50 pm

In this article we are going to see simple a script to display URL parameters using Javascript. We are basically getting the query string value by passing the name of the query string to a custom function. As you can see the function we are using to filter the url parameter is getmyuri(), this basically extracts URI parameter with the help of regular expressions.

<script type="text/javascript">
var myStr = 'http://www.mistonline.in/process?id=2193&name=john';
document.write ( getmyuri('name', myStr) );
// outputs: john
document.write("<br>");
document.write ( getmyuri('id', myStr) );
// outputs: 2193

function getmyuri(n,s){
n = n.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");
var p = (new RegExp("[\\?&]"+n+"=([^&#]*)")).exec(s);
return (p===null) ? "" : p[1];
}
</script>

Just pass the name of the query string and the URL to the function and boom we got the data

From this you can extract the desired parameter from browser URL.
Thanks 🙂 Enjoy!!!!!!

Demo

Leave a Reply

Your email address will not be published. Required fields are marked *