3

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

window.location.host // you'll get sub.domain.com:8080 or sub.domain.com:80
window.location.hostname // you'll get sub.domain.com
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.com/testsite/index.html

My PROD site name is example.com/index.html

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

However for QA I need to return example.com/testsite

For PROD i need to return example.com

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

3
  • You already have all the information you need... window.location.hostname + window.location.pathname I don't understand what is the problem :/ Commented Jul 6, 2017 at 9:38
  • thanks for your comment, but i dont know whether i am on QA or PROD at code side. to manupulate the actual path Commented Jul 6, 2017 at 9:46
  • 1
    The title of your question does not represent the problem you are facing Commented Jul 6, 2017 at 9:51

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks @Rory for reply, but there is not a fix name like 'testsite' for folder under my site
Your question says there is...?
Ok, but then just change the string to whatever it should be. The logic is the same
2

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

Comments

0

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]

2 Comments

But what about getting the /testsite path in the response?
thanks @Satinder for Your Response, but if for QA '(localhost/testsite/test/form.html)' in this case it will work. but PROD '(localhost/test/form.html)' it will give wrong finalurl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.