• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

Conditionally Add www To Url In Javascript

How can we add www. to the url if missing, in the javascript below, that is included on our shopping cart pages?

For example: http://mydomain.com would result in https://www.mydomain.com

Thanks in advance.
<script language="javscript" type="text/javascript" xml:space="preserve">
//<![CDATA[
if ( location.protocol != 'https:' ) {
	window.location= 'https://' + location.host + location.pathname + location.search
}
//]]>
</script>

Open in new window

0
jahlife
Asked:
jahlife
  • 3
  • 2
  • 2
1 Solution
 
Chad SmithSoftware EngineerCommented:
if ( location.host.substring(4) != 'www.' ) {
        window.location= location.protocol +'www.' + location.host + location.pathname + location.search
}
0
 
OriNetworksCommented:
What you would want to do is use indexOf to see if the string www. exists. Here is how to use indexOf
http://www.w3schools.com/jsref/jsref_IndexOf.asp
0
 
jahlifeAuthor Commented:
OK, can you assist with refining this code below based on your suggested direction?  I need to make sure case sensitivity is handled, too.

Thanks for feedback.

<script language="javscript" type="text/javascript" xml:space="preserve">
//<![CDATA[

var str_host = location.host;

if ( str_host.indexOf("www.") != "-1" ) {
      str_host = "www." + str_host
}

if ( location.protocol != 'https:' ) {
      window.location= 'https://' + str_host + location.pathname + location.search
}
//]]>
</script>

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
jahlifeAuthor Commented:
I just changed doubles to singles, below.  Is this on the right track?

var str_host = location.host;

if ( str_host.indexOf('www.') != '-1' ) {
      str_host = 'www.' + str_host;
}

if ( location.protocol != 'https:' ) {
      window.location= 'https://' + str_host + location.pathname + location.search
}
0
 
Chad SmithSoftware EngineerCommented:
<script language="javscript" type="text/javascript" xml:space="preserve">
//<![CDATA[

var str_host = location.host;

if ( str_host.toLowerCase().indexOf("www.") != "-1" ) {
      str_host = "www." + str_host
}

if ( location.protocol != 'https:' ) {
      window.location= 'https://' + str_host + location.pathname + location.search
}
//]]>
</script>
0
 
jahlifeAuthor Commented:
Thanks again.
0
 
OriNetworksCommented:
Considering I pointed in the correct direction and hoped you would do some of the work it would have been nice to get some credit as an assist. Not a big deal but in the future, try to credit others on experts-exchange with points if they contribute to the final solution.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now