Get URL parameters using Javascript
With PHP, We easily get the URL by $_GET method. With JavaScript is not so easy but you still can do that with function below:
1 2 3 4 5 6 7 8 9 10 | function GetURLParameter(sParam){ var sPageURL = window.location.search.substring(1); var sURLVariables = sPageURL.split('&'); for (var i = 0; i < sURLVariables.length; i++){ var sParameterName = sURLVariables[i].split('='); if (sParameterName[0] == sParam){ return sParameterName[1]; } } } |
Now you can using this function get URL parameter.
Example you want get parameter from “name” parameter in URL: http://domain.com/index.php?name=Kiet. And this is code:
1 | var name = GetURLParameter('name'); |
Good luck!