使函数在HTTP和HTTPS中工作
Making a function work in HTTP and HTTPS
我有从另一个开发人员那里继承的Javascript代码。我对Javascript很陌生。
我遇到的问题是,当用户在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>");
}
});
});
使用 window.location 确定用户的当前协议并相应地进行调整:
var tier1CategoryLink = window.location.protocol + "//" + window.location.hostname + "/categories/";
或者只使用相对网址:
var tier1CategoryLink = "/categories/";
我想你想说你在浏览器中遇到了一个java脚本错误。这也是意料之中的。当用户在https中时,您必须修改java脚本以包含https。例如,您的第一行必须如下所示:
var tier1CategoryLink = "https://" + window.location.hostname + "/categories/";
编辑:此外,您可以查看此内容以解决您的问题。检测HTTP或HTTPS,然后在JavaScript中强制HTTPS
最简单的解决方案是只使用双斜杠
var tier1CategoryLink = "//" + window.location.hostname + "/categories/";
规范第 5.2 节中的详细信息
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- jQuery UI自动完成突然停止工作
- 谷歌图书预览没有在安全的https上显示图书图像链接,但在http上工作.在Google Chrome和Touch浏览器
- 脚本无法通过 https 工作
- 使函数在HTTP和HTTPS中工作
- 使用 js 调整 iframe 的大小在 https 时停止工作
- 任何使postMessage与HTTPS一起工作的方法
- 我的节点.js https 客户端始终工作,无论证书有效性如何
- 使用Web浏览器控件和https时出现Javascript错误,但它在IE中工作
- 如何使谷歌免费小部件与https一起工作
- 如何工作zRSSFeed与https
- BxSlider不工作的https在IE(混合内容警告)
- JavaScript按钮不能与HTTPS连接一起工作
- Iframe与HTTPS视频url不工作
- Javascript表单提交不工作在https
- HTTPS在ie:弹出窗口不能正常工作(因为脚本)
- 为什么JavaScript不工作在我的网站上的HTTPS
- 下拉列表don'不能在HTTPS中工作,但在HTTP中工作良好
- Disqus不能在chrome的HTTPS页面上工作.任何修正
- Cookies不能在https:// why中工作