admin管理员组

文章数量:1357377

I get too many ways to get the hostname like code below:

window.location.host // you'll get sub.domain:8080 or sub.domain:80
window.location.hostname // you'll get sub.domain
window.location.protocol // you'll get http:
window.location.port // you'll get 8080 or 80
window.location.pathname // you'll get /virtualPath

In my case I want something different. For example:

My QA site name is example/testsite/index.html

My PROD site name is example/index.html

The problem here using the above methods to get the hostname it is returning me only the hostname like this: example

However for QA I need to return example/testsite

For PROD i need to return example

Is it possible with the single code? Thanks in advance.

I get too many ways to get the hostname like code below:

window.location.host // you'll get sub.domain.:8080 or sub.domain.:80
window.location.hostname // you'll get sub.domain.
window.location.protocol // you'll get http:
window.location.port // you'll get 8080 or 80
window.location.pathname // you'll get /virtualPath

In my case I want something different. For example:

My QA site name is example./testsite/index.html

My PROD site name is example./index.html

The problem here using the above methods to get the hostname it is returning me only the hostname like this: example.

However for QA I need to return example./testsite

For PROD i need to return example.

Is it possible with the single code? Thanks in advance.

Share Improve this question edited Jul 6, 2017 at 9:57 Ganesh Londhe asked Jul 6, 2017 at 9:36 Ganesh LondheGanesh Londhe 1611 gold badge1 silver badge12 bronze badges 3
  • You already have all the information you need... window.location.hostname + window.location.pathname I don't understand what is the problem :/ – vsync Commented Jul 6, 2017 at 9:38
  • thanks for your ment, but i dont know whether i am on QA or PROD at code side. to manupulate the actual path – Ganesh Londhe Commented Jul 6, 2017 at 9:46
  • 1 The title of your question does not represent the problem you are facing – vsync Commented Jul 6, 2017 at 9:51
Add a ment  | 

3 Answers 3

Reset to default 3

To achieve what you require you'll need to check the window.location.hostname, and also the first folder in the window.location.pathname. Something like this:

function getPath() {
  var folder = (window.location.pathname.split('/')[0] || '').toLowerCase() == 'testsite' ? '/testsite' : '';
  return window.location.hostname + folder;
}

Best method that works for both PROD & QA

var BASE_URL = window.location.href;
    BASE_URL = BASE_URL.split("testsite");
    if (BASE_URL.length > 1)
    {
        BASE_URL = BASE_URL[0];
        BASE_URL = BASE_URL + 'testsite';
    } else{
        BASE_URL = window.location.origin;
   }

Use window.location.hostname;

Example:
Page URL is http://localhost:2239/Default2.aspx?id=5&name=SatinderSingh

var getCurrentURL =window.location.href; //http://localhost:2239/Default2.aspx?id=5&name=SatinderSingh
var getHostname=window.location.hostname; //localhost
var getPathName=window.location.pathname  // Default2.aspx
var getPortNo=window.location.port        // 2239
var getQueryString=window.location.search //?id=5&name=SatinderSingh

   var getHostname = window.location.hostname; //localhost
   var getPathName = window.location.pathname  // Default2.aspx
   var split_PathName = String(getPathName.split("/"));        
   var FinalURL = getHostname + "/" + split_PathName[1]

本文标签: jqueryget the hostname(Base URL) using javascriptStack Overflow