2

I have this Javascript code I inherited from another developer. I am very new to Javascript.

The problem I'm having is that it won't work when the user is in HTTPS. Is there a workaround for this problem?

var tier1CategoryLink = "http://" + window.location.hostname + "/categories/";
    $("#as_Placeholder").load(tier1CategoryLink + " .SubCategoryList > ul", function(){
        $('#tier1').find('option').remove().end().append('<option>Make</option>');
        $("#as_Placeholder ul li").each(function(){
            var thisText = $(this).children("a").text();
            if ((thisText != "All Products") && (thisText != "Best Selllers") && (thisText != "Chromoly Flywheel Combo Sale") && (thisText != "New Arrivals") && (thisText != "On Sale") && (thisText != "Needs Categories")) {
                $("#tier1").append("<option value='" + $(this).children("a").attr("href") + "'>" + thisText + "</option>");
            }
        });
    });
Jack Pilowsky
  • 2,275
  • 5
  • 28
  • 38
  • 1
    Well, does it "work" if `"http://"` is changed to `"https://"`? (The first step to solving a problem is to understand the problem.) –  Jun 15 '12 at 17:00
  • 1
    define "the user is in HTTPS" – PA. Jun 15 '12 at 17:02
  • you may want to use relative links (or absolute relative I think it's called). Basically, just do `/categories` instead of `http://.../categories`. That way the http or https are automatically done (as long as you don't go from a non-secure to a secure page for example, in which case you do need to specify it). Just change the first line to `var tier1CategoryLink = "/categories/";` – Rodolfo Jun 15 '12 at 17:10

3 Answers3

3

Use window.location to determine user's current protocol and adjust accordingly:

var tier1CategoryLink = window.location.protocol + "//" + window.location.hostname + "/categories/";

Or just use relative URL:

var tier1CategoryLink = "/categories/";
Oleg V. Volkov
  • 21,719
  • 4
  • 44
  • 68
0

I guess you want to say that you get a java-script error in the browser. And that is expected too. When the user is in https then you have to amend the java-script to include https. For eg your first line must look like this:

var tier1CategoryLink = "https://" + window.location.hostname + "/categories/";

EDIT: Moreover you can have a look at this to solve your issue. Detect HTTP or HTTPS then force HTTPS in JavaScript

Community
  • 1
  • 1
dharam
  • 7,882
  • 15
  • 65
  • 93
0

Easiest solution is to just use double slashes

var tier1CategoryLink = "//" + window.location.hostname + "/categories/";

Details in spec Section 5.2

epascarello
  • 204,599
  • 20
  • 195
  • 236