JavaScript (JS) get current domain name URL program code

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.