Jack Pilowsky
Jack Pilowsky

Reputation: 2303

Making a function work in HTTP and HTTPS

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>");
            }
        });
    });

Upvotes: 2

Views: 767

Answers (3)

epascarello
epascarello

Reputation: 207531

Easiest solution is to just use double slashes

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

Details in spec Section 5.2

Upvotes: 0

Oleg V. Volkov
Oleg V. Volkov

Reputation: 22421

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/";

Upvotes: 3

dharam
dharam

Reputation: 8096

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

Upvotes: 0

Related Questions