Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
133
rated 0 times [  134] [ 1]  / answers: 1 / hits: 41502  / 14 Years ago, wed, january 5, 2011, 12:00:00

What's the difference between using



if (document.domain.toLowerCase().indexOf(domainName) != -1)


and



if(window.location.href.match(/://(.[^/]+)/)[1].toLowerCase().indexOf(domainName) != -1)


and



if(window.location.hostname.toLowerCase().indexOf(domainName) != -1)


I'm just trying to match on a certain domainName and want to use the best approach.


More From » dns

 Answers
116

Best and most readable would be:



if(location.hostname == mysite.com){

}





Update:



Or as Patrick pointed out if you are only looking for part of the domain name I would use match.



if(location.hostname.match('mysite')){} // will return null if no match is found

[#94358] Tuesday, January 4, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yulisa

Total Points: 436
Total Questions: 102
Total Answers: 123

Location: Palau
Member since Tue, May 30, 2023
1 Year ago
yulisa questions
Sun, Jun 20, 21, 00:00, 3 Years ago
Wed, Apr 14, 21, 00:00, 3 Years ago
Fri, Aug 7, 20, 00:00, 4 Years ago
Mon, Mar 23, 20, 00:00, 4 Years ago
;