码迷,mamicode.com
首页 > 移动开发 > 详细

iOS: UIWebView 中不加载图片(即浏览器常见的无图模式)

时间:2015-09-01 19:46:42      阅读:864      评论:0      收藏:0      [点我收藏+]

标签:

写到关于UIWebView不加载图片时,查了很多资料,

关于js,关于oc,在stackoverflow 等 并没有找到合适的答案,

最终觉得还是应该回归传统 用js代码,

stringByEvaluatingJavaScriptFromString 来调用js方法

js代码:

var div_hide_css_id = "iBrowser_div_hide_css";
// ------------------- 有图/无图 ----

function JSHandleHideImage () {
    var i = 0;
    var allDiv = document.getElementsByTagName("DIV");
    for (i; i<allDiv.length; i++) {
        var e = allDiv[i];
        var bg_image = e.style.backgroundImage;
        e.style.backgroundImage = "none";
        e.setAttribute("bg_image", bg_image);
    }
    
    var newCss = document.getElementById(div_hide_css_id);
    
    if(newCss == undefined){
        document.documentElement.innerHTML= document.documentElement.innerHTML+"<style id=‘"+div_hide_css_id+"‘>img{visibility:hidden;}</style>";
    }
    else {
        if(newCss.innerHTML == ‘‘) {
            
        }
        newCss.innerHTML = img{visibility:hidden;};
    }
}
function JSHandleShowImage () {
    var i = 0;
    var allDiv = document.getElementsByTagName("DIV");
    for (i; i<allDiv.length; i++) {
        var e = allDiv[i];
        var bg_image = e.getAttribute("bg_image");
        e.style.backgroundImage = bg_image;
    }

    var newCss = document.getElementById(div_hide_css_id);
    if(newCss){
        newCss.innerHTML = img{visibility:visibility;};
    }
}

oc代码

if (//无图) {
    [webView stringByEvaluatingJavaScriptFromString:@"JSHandleHideImage()"];
  }else{
    [webView stringByEvaluatingJavaScriptFromString:@"JSHandleShowImage()"];
  }

 

iOS: UIWebView 中不加载图片(即浏览器常见的无图模式)

标签:

原文地址:http://www.cnblogs.com/yoon/p/4776572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!