Example Document.domain
The code is as follows |
Copy Code |
<script language= "JavaScript" > Get Domain Name host = Window.location.host; Host2=document.domain; Get full address of page url = window.location.href; document.write ("<br>host=" +host) document.write ("<br>host2=" +host2) document.write ("<br>url=" +url) </script> |
Method Two, using the URL to resolve the domain name
The code is as follows |
Copy Code |
<script type= "Text/javascript" > function gethost (URL) { var host = "NULL"; if (typeof url = = "undefined" | | | null = = URL) { url = window.location.href; } var regex =/^\w+\:\/\/([^\/]*). * *; var match = Url.match (regex); if (typeof match!= "undefined" && null!= match) { host = match[1]; } return host; }
</script> |
How to use:
The code is as follows |
Copy Code |
<script type= "Text/javascript" > var url = ' Http://www.111cn.net '; Alert (gethost (URL)); </script> |
Add
4 ways to get the current URL
var url = window.location.href;
var url = self.location.href;
var url = document. URL;
var url = document.location;
1. Get the current full URL
Thisurl = document. URL;
Thishref = Document.location.href;
Thissloc = Self.location.href;
Thisdloc = document.location;
Strwrite = "Thisurl: [" + Thisurl + "]"
Strwrite + = "Thishref: [" + Thishref + "]"
Strwrite + = "Thissloc: [" + Thissloc + "]"
Strwrite + = "Thisdloc: [" + Thisdloc + "]"
document.write (Strwrite);
2. Get the current domain name information
Thistloc = Top.location.href;
Thisploc = parent.document.location;
Thisthost = Top.location.hostname;
Thishost = Location.hostname;
Strwrite = "Thistloc: [" + Thistloc + "]"
Strwrite + = "Thisploc: [" + Thisploc + "]"
Strwrite + = "Thisthost: [" + Thisthost + "]"
Strwrite + = "Thishost: [" + Thishost + "]"
document.write (Strwrite);
3. Get the current page
Tmphpage = Thishref.split ("/");
Thishpage = tmphpage[Tmphpage.length-1];
Tmpupage = Thisurl.split ("/");
Thisupage = tmpupage[Tmpupage.length-1];
Strwrite = "Thishpage: [" + Thishpage + "]"
Strwrite + = "Thisupage: [" + Thisupage + "]"
document.write (Strwrite);
Introduction to Location Properties:
HREF Sets or gets the entire URL as a string.
Search sets or gets the part of the HREF attribute that follows the question mark.
Hash sets or gets the portion of the HREF attribute that follows the number "#" in the.
Protocol sets or gets the protocol portion of the URL.
Host sets or gets the hostname and port number of the location or URL.
Hostname to set or get location or URL hostname
Port Sets or gets the number of ports associated with the URL.
Pathname Sets or gets the filename or path specified by the object.