Using JavaScript to get the URL of the current page is a complicated question, and if you think about it for the first time, many people are wondering what kind of javascript function it is.
Actually, the function that JavaScript gets the URL of the current page is the window.location.href that we often use to redirect.
For example, the following function:
<script>var Url=window.location.href;var loc = url.substring (Url.lastindexof ('/') +1, url.length); Alert ("Url=" +url+ ", loc=" +loc);</script>
If the URL of the current page is http://localhost:6666/myphp/mobile/pc1.php, then it runs as follows:
The above function shows how to get the current page name.
After obtaining the entire URL using the variable of window.location.href, which part of the address to use, using string processing functions such as substring,indexof to handle the obtained URL, intercept the part you want.
Usually use Window.location.href to redirect, is to change the URL of the entire browser,
If there is no assignment later, this becomes the statement that gets the current value.
As with document.getElementById ("XX"). Value, you can modify it to get it.
Of course, if you get some specific value, such as the current protocol and port, you don't have to intercept the string.
Directly using the Window.location.port port, Window.location.protocol get the protocol, Window.location.search can also take the part of the URL question mark, of course, you can also use them to change the corresponding value.
For example, the following code:
Alert ("Protocol:" +window.location.protocol+ ", Port:" +window.location.port);
The results of the operation are as follows:
"JavaScript" gets the URL of the current page with the Window.location.href