I use "_spPageContextInfo.webAbsoluteUrl
" to detect the site url.
- From root site:
_spPageContextInfo.webAbsoluteUrl
, the result is "http://mywebsite:8888" - From subsite level 1:
_spPageContextInfo.webAbsoluteUrl
, the result is "http://mywebsite:8888/subsite1" - From subsite level 2:
_spPageContextInfo.webAbsoluteUrl
, the result is "http://mywebsite:8888/subsite1/subsite1_1"
How to identity your current site which you are surfing is root site or subsite by javascript? If subsite, which level of subsite is it? The solution is only javascript and only bases on the site url.
Thanks for helping.
You can use
_spPageContextInfo.siteServerRelativeUrl
.If
_spPageContextInfo.siteServerRelativeUrl
equals to/
then you are definitely in the root site.In the subsite, the value of
_spPageContextInfo.siteServerRelativeUrl
would be/subsite1
.In the sub-subsite, the value of
_spPageContextInfo.siteServerRelativeUrl
would be/subsite1/subsite1_1
.Another option would be, to check if
_spPageContextInfo.webAbsoluteUrl
equals to_spPageContextInfo.siteAbsoluteUrl
. This will be true only in case of the root site. So,if(_spPageContextInfo.webAbsoluteUrl == _spPageContextInfo.siteAbsoluteUrl)
, then you would be in the root site.To detect the level of subsite, you can try it as below: