JavaScript basics
Use JavaScript to get the current URL and other URL parameters simple syntax
In PHP , we often need to get URL parameters, but not every time we use PHP to get URL parameters, because sometimes some actions only need to be performed on the client side, so this article mainly shares how to get URLs and URLs through JavaScript. The parameters of other URLs are very useful tips.
First, the syntax for obtaining the current web page URL through JavaScript is location.href, the following simple example
<script type="text/javascript">alert(location.href);</script>
In this example, we use alert, so a dialog window will pop up to tell you the current URL. Of course, you can also use document.write to present it, just to avoid the problem of garbled webpages, so here we use alert syntax to present it.Suppose our website is: http://www.wibibi.com:80/test.html?tid=222#333
immediately write a simple example to get all the website information
<script type="text/javascript">
alert('location.href: '+location.href); //The output value is: location.href: http://www.wibibi.com/test.html?tid=222#333
alert('location.protocol: '+location.protocol); //The output value is: location.protocol: http:
alert('location.hostname: '+location.hostname); //The output value is: location.hostname: www.wibibi.com
alert('location.host: '+location.host); //The output value is: location.host: www.wibibi.com
alert('location.port: '+location.port); //The output value is: 80
alert('location.pathname: '+location.pathname); //The output value is: location.pathname: /test.html
alert('location.search: '+location.search); //The output value is: location.search: ?tid=222
alert('location.hash: '+location.hash); //The output value is: location.hash: #333
</script>
alert('location.href: '+location.href); //The output value is: location.href: http://www.wibibi.com/test.html?tid=222#333
alert('location.protocol: '+location.protocol); //The output value is: location.protocol: http:
alert('location.hostname: '+location.hostname); //The output value is: location.hostname: www.wibibi.com
alert('location.host: '+location.host); //The output value is: location.host: www.wibibi.com
alert('location.port: '+location.port); //The output value is: 80
alert('location.pathname: '+location.pathname); //The output value is: location.pathname: /test.html
alert('location.search: '+location.search); //The output value is: location.search: ?tid=222
alert('location.hash: '+location.hash); //The output value is: location.hash: #333
</script>
Post a Comment
0 Comments