从字符串URL获取域和页名

时间:2022-08-23 10:37:30

Well i'm currently have some issue about manipulating an URL.

我现在有一些关于操作URL的问题。

Technically what i want is to get the domain name and the page name from a page.

从技术上讲,我想要的是从一个页面中获取域名和页面名称。

For example :

例如:

www.myWebSite.com => domain : myWebSite
http://myWebSite.com => domain : myWebSite
myWebSite.com/xxx.hmtl => domain : myWebSite page : xxx

5 个解决方案

#1


18  

window.location.hostname; //Domain name

$("title").text(); //Page name

EDIT:

编辑:

var loc = window.location;

var filename = loc.pathname.split("/");
filename = filename[pathname.length-1];

alert("Domain: "+loc.hostname);
alert("Filename: "+filename);

#2


13  

try with /:\/\/(.[^/]+)/.match(url)[1]

尝试用/:\ \ /([^ /]+)/ .match(url)[1]

example :

例子:

var r = /:\/\/(.[^/]+)/;
"http://*.com/questions/5343288/get-the-domain-and-page-name-from-a-string-url".match(r)[1] 
=> *.com

#3


4  

Use window.location.hostname or window.location.host. Check location reference.

使用window.location。主机名或window.location.host。检查位置参考。

#4


1  

I hope this helps:

我希望这有助于:

function breakDownURL(url) {
    var domain = "",
        page = "";
    //remove "http://"
    if (url.indexOf("http://") == 0) {
        url = url.substr(7);
    }
    //remove "www."
    if (url.indexOf("www.") == 0) {
        url = url.substr(4);
    }
    domain = url.split('/')[0].split('.')[0]
    if (url.split('/').length > 1) {
        page = url.split('/')[1].split('.')[0];
    }
    document.write("domain : " + domain + 
      (page == "" ? "" : " page : " + page) + page + "<br/>");
}

breakDownURL("www.myWebSite.com"); // domain : myWebSite
breakDownURL("http://myWebSite.com"); // domain : myWebSite
breakDownURL("myWebSite.com/xxx.html"); // domain : myWebSite page : xxx

#5


0  

var url = window.location.href;  //www.myWebSite.com/myWebSite
var arr = url.split("/");
var page = arr[arr.length-1];
var domain = window.location.host;
alert(domain);                  //www.myWebSite.com
var n = page.includes("?");     // if www.myWebSite.com/myWebSite?parameter
if(n)
{
var page_arr = page.split("?");
var page = page_arr[0];        //myWebSite
}
alert(page);                   //myWebSite

#1


18  

window.location.hostname; //Domain name

$("title").text(); //Page name

EDIT:

编辑:

var loc = window.location;

var filename = loc.pathname.split("/");
filename = filename[pathname.length-1];

alert("Domain: "+loc.hostname);
alert("Filename: "+filename);

#2


13  

try with /:\/\/(.[^/]+)/.match(url)[1]

尝试用/:\ \ /([^ /]+)/ .match(url)[1]

example :

例子:

var r = /:\/\/(.[^/]+)/;
"http://*.com/questions/5343288/get-the-domain-and-page-name-from-a-string-url".match(r)[1] 
=> *.com

#3


4  

Use window.location.hostname or window.location.host. Check location reference.

使用window.location。主机名或window.location.host。检查位置参考。

#4


1  

I hope this helps:

我希望这有助于:

function breakDownURL(url) {
    var domain = "",
        page = "";
    //remove "http://"
    if (url.indexOf("http://") == 0) {
        url = url.substr(7);
    }
    //remove "www."
    if (url.indexOf("www.") == 0) {
        url = url.substr(4);
    }
    domain = url.split('/')[0].split('.')[0]
    if (url.split('/').length > 1) {
        page = url.split('/')[1].split('.')[0];
    }
    document.write("domain : " + domain + 
      (page == "" ? "" : " page : " + page) + page + "<br/>");
}

breakDownURL("www.myWebSite.com"); // domain : myWebSite
breakDownURL("http://myWebSite.com"); // domain : myWebSite
breakDownURL("myWebSite.com/xxx.html"); // domain : myWebSite page : xxx

#5


0  

var url = window.location.href;  //www.myWebSite.com/myWebSite
var arr = url.split("/");
var page = arr[arr.length-1];
var domain = window.location.host;
alert(domain);                  //www.myWebSite.com
var n = page.includes("?");     // if www.myWebSite.com/myWebSite?parameter
if(n)
{
var page_arr = page.split("?");
var page = page_arr[0];        //myWebSite
}
alert(page);                   //myWebSite