2012-06-15 28 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

相關問題