Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am having below code and I want sub site name from the current site url.

It is working fine if url does not contains # in it.
But it fails if url contains #HR or #IT at end of the url for anchor tags.
Here is my code.


JavaScript
var currentURL = $(location).attr('href');
var currentSite = $(location).attr('href').replace($(location).attr('pathname'), '');
var siteStr = currentURL.replace(currentSite, '');
var subSite = siteStr.substring(1, 3).toUpperCase();

if (subSite == "HR") {
// here is my code if subsite = HR
}

else if (subSite == "IT") {
// here is my code if subsite = IT
}

else
{
// here is my code if subsite is other than IT and HR
}


Thanks,
Posted
Updated 2-Jun-15 1:49am
v2

1 solution

You should actually be using subSite.indexOf("HR")>-1 or something along those lines so that you are testing for a certain string within the url, instead of trying to match it up directly. That way if there is a query string you'll still work.
 
Share this answer
 
Comments
Rashmi Sutar 3-Jun-15 3:33am    
Hi,

but in "var subSite" I am getting "tt" as string so its not checking remaining things.

Thanks,
ZurdoDev 3-Jun-15 7:18am    
I don't understand what your question is.
Rashmi Sutar 3-Jun-15 9:09am    
hi RyanDev,

When I am running this script the value in subSite is "tt" so it is not matching with "HR" or "IT"
ZurdoDev 3-Jun-15 9:10am    
OK. What is the question though?
Rashmi Sutar 3-Jun-15 9:14am    
I want to get the subsite name and wants to check as shown in if else statements and need to do further action.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900