如何使用 angularjs 检测混合内容
How to detect mixed content with angularjs?
我有一个angularjs
应用程序运行在tomcat
上,并且在一个loadbalancer
后面
当然,如果应用程序是通过具有https
的负载均衡器请求的,则平衡器仍然通过http
在内部请求应用程序。
问题:在这种情况下,我想隐藏一个显示混合内容的选项卡(因为我必须嵌入不支持https的外部pdf链接,因此我想隐藏它们)。
我无法使用$location.protocol()
因为该应用程序位于负载均衡器后面,并且总是只能http
。
问题:我是否有机会检测到浏览器是否实际显示mixed content
?
您无法以简单的方式检测到它。您可以尝试在 iframe 上侦听加载事件并设置超时,当超时触发时,阻止 iframe,因为 iframe 没有像这样加载(jsfiddle 示例):
checkMixedContent(urlToCheck, function(urlToCheck) {
// For example, change location
alert('ok');
// load iframe
}, function() {
alert('Error: resource timed out');
// hide iframe / show message
}, checkDelay);
function checkMixedContent(urlToCheck, successCallback, errorCallback, checkDelay, dontCheckOnError) {
checkDelay = checkDelay || 10000;
// 1. Create invisible iframe and append it to body
var iframeHelper = document.createElement("iframe");
iframeHelper.src = urlToCheck;
iframeHelper.height = 0;
iframeHelper.width = 0;
iframeHelper.style.visibility = 'hidden';
document.body.appendChild(iframeHelper);
// 2. Set time out and while content on iframeHelper.src should be definitely loaded
var checkTimeout = window.setTimeout(function() {
errorCallback(urlToCheck);
}, checkDelay);
var onLoad = function() {
window.clearTimeout(checkTimeout); // if OK - not show error => clearTimeout
iframeHelper.removeEventListener('load', onLoad);
iframeHelper.removeEventListener('error', onError);
document.body.removeChild(iframeHelper);
successCallback(urlToCheck);
};
var onError = function() {
window.clearTimeout(checkTimeout); // if OK - not show error => clearTimeout
iframeHelper.removeEventListener('load', onLoad);
iframeHelper.removeEventListener('error', onError);
document.body.removeChild(iframeHelper);
errorCallback(urlToCheck);
};
// 3. If everything is fine - "load" should be triggered
iframeHelper.addEventListener('load', onLoad);
// Turn "true" in case of "X-Frame-Options: SAMEORIGIN"
if (!dontCheckOnError) {
iframeHelper.addEventListener('error', onError);
}
}
相关文章:
- 如何检测是否有溢出
- 如何检测用于WebGL的专用或集成显卡
- 防止Alt+Shift默认操作或检测多种操作系统语言的Javascript
- 如何在使用selectBoxIt JQuery插件时检测选项更改
- 如何使用密码检测网络中的状态连接
- 使用JavaScript检测Flash
- 在不阻止默认行为的情况下检测IE10中的缩放
- 检测个位数整数时正在转换毫秒
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 动态插入的表:JQuery未检测到最后一行
- 混合 ui-sref 和 $state.go 在 Angular ui-router 中进行状态转换
- 检测页面上某个元素中选择(突出显示)或单击的内容
- jQuery检测浏览器是否支持Zoom
- 检测iframe是否跨域的愚蠢方法
- Javascript:检测Internet Explorer是否发送了https页面上的混合http消息
- 如何使用 angularjs 检测混合内容
- 使用 JavaScript 或 jQuery 检测 Firefox 混合内容阻塞
- 有没有办法检测对混合模式的支持
- 如何在phonegap混合应用程序中检测URL方案并防止默认导航到该URL
- 检测混合设备