I have the below JavaScript, and when the url (window.location) does not contain www. the javascript IS executed
var windowloc = window.location; // http://mywebsite.com/
var homeurl = "http://mywebsite.com/";
if(windowloc==homeurl){
//JavaScript IS EXECUTED
}
and if it does the javascript is not executed.
var windowloc = window.location; // http://www.mywebsite.com/
var homeurl = "http://mywebsite.com/";
if(windowloc==homeurl){
//JavaScript is NOT executed.
}
How can I overcome this by allowing the JavaScript to accept urls (window.location) with and without www.
You can overcome that using regex, as I am sure other answers will provide. However, it’s best practice for search engine optimization (SEO) to force your
http://mywebsite.com/to do a perminant redirect tohttp://www.mywebsite.com/because search engines like Google consider the www. and www-less versions two separate websites.Then you will not need two separate conditions because your url will always be the www. version.