2012-06-15 31 views
2

我有这个Javascript代码,我从其他开发人员继承。我对JavaScript很陌生。使功能在HTTP和HTTPS中工作

我遇到的问题是,当用户处于HTTPS中时,它不起作用。有没有解决这个问题的方法?

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

定义:“will not work” – saluce

+1

好吧,如果'“http://”'改为'“https://”',它会“工作”吗? (解决问题的第一步是了解问题。) – 2012-06-15 17:00:22

+1

定义“用户处于HTTPS” –

回答

3

使用了window.location来确定用户当前的协议,并进行相应的调整:

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

或者只是使用相对URL:

var tier1CategoryLink = "/categories/"; 
0

我想你想说,你在浏览器中得到一个java脚本错误。这也是预料之中的。当用户使用https时,您必须修改java脚本以包含https。对于例如,您的第一行必须是这样的:

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

编辑:此外,你可以看看这个解决您的问题。 Detect HTTP or HTTPS then force HTTPS in JavaScript

0

最简单的解决方法就是使用双斜线

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

详细规格Section 5.2

相关问题