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
3 Answers
Reset to default 3To 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
版权声明:本文标题:jquery - get the hostname(Base URL) using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744080075a2587466.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论