Sindar
Sindar

Reputation: 10839

Get the domain and page name from a String URL

Well i'm currently have some issue about manipulating an 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

Upvotes: 17

Views: 40211

Answers (6)

Hitsuki
Hitsuki

Reputation: 41

You can write something like my function below.

function getHostnameFromURI(url) {
    // Check slashes in URL
    if (url.indexOf('?') == -1 && !url.endsWith('/') || url.indexOf('#') == -1 && !url.endsWith('/')) {
        url += '/';
    }
    
    let start = url.indexOf('://')+3;
    let end = url.indexOf('/', start);

    let domain = url.slice(start, end);
    
    // Remove port specification from the URL
    if (domain.indexOf(':') != -1) {
        domain = domain.slice(0, domain.indexOf(':'));
    }

    return domain;
}

It correctly works with localhost, .co.uk, etc. domains.

https://stackoverflow.com/opensearch.xml --> stackoverflow.com

https://github.com/arichr --> github.com

Information: Please, check that your URLs are not wrong e.g. https://example.com?a=a will return example.com?a=a

Upvotes: 0

nguyên
nguyên

Reputation: 5326

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

example :

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

Upvotes: 15

mattsven
mattsven

Reputation: 23293

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);

Upvotes: 18

Chandrika Prasad Shah
Chandrika Prasad Shah

Reputation: 773

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

Upvotes: 0

Lewis Nakao
Lewis Nakao

Reputation: 7372

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

Upvotes: 1

amit_g
amit_g

Reputation: 31260

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

Upvotes: 5

Related Questions