Example analysis of JS's method of obtaining url parameters and main domain name

  • 2021-07-09 06:39:31
  • OfStack

This paper describes the method of obtaining url parameters and main domain name by JS. Share it for your reference, as follows:


<script>
alert(location.hostname)
alert(location.port)
alert(location.pathname)
alert(location.search)
</script>
<script>
function winsx(url) //URL Parameters passed 
{
 var winurl =url?url:(unescape(window.location.href));
 var wlp = winurl.split("?")[1];
 var wincs = wlp.split("&");
 for(var i=0; i<wincs.length; i++)
 {
  var tur = wincs[i].split("=");
  eval('this.'+tur[0]+'="'+tur[1]+'";');
 }
}
var ug = new winsx("http://www.yourname.com/aa/bb.php?val=11&test=2");
alert(ug.val)
alert(ug.test)
</script>

More readers interested in JavaScript can check out the topics of this site: "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Data Structure and Algorithm Skills", "Summary of JavaScript Traversal Algorithm and Skills", "Summary of json Operation Skills in JavaScript", "Summary of JavaScript Switching Effects and Skills", "Summary of JavaScript Animation Effects and Skills", "Summary of JavaScript Error and Debugging Skills" and "Summary of JavaScript Mathematical Operation Usage"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: