烟雨江南
烟雨江南
  • 发布:2015-04-15 14:15
  • 更新:2015-04-15 14:48
  • 阅读:3821

关于hello h5+的clicked方法使用问题

分类:HTML5+
mui
<li onclick="clicked(this.id)" class="mui-table-view-cell mui-media mui-col-xs-4 mui-col-sm-3" id="a.html">  
<a>  
<span class="mui-icon mui-icon-contact"></span>  
<div class="mui-media-body">档案</div>  
</a>  
</li>  
<script type="text/javascript" charset="utf-8">  
             //取消浏览器的所有事件,使得active的样式在手机上正常生效  
            document.addEventListener('touchstart', function() {  
                return false;  
            }, true);  
             // 禁止选择  
            document.oncontextmenu = function() {  
                return false;  
            };  
             // H5 plus事件处理  
            var as = 'slide-in-right',  
                at = 200; // 默认动画时间  
            function plusReady() {  
                // 隐藏滚动条  
                plus.webview.currentWebview().setStyle({  
                    scrollIndicator: 'none'  
                });  
                // Android处理返回键  
                plus.key.addEventListener('backbutton', function() {  
                    if (confirm('确认退出?')) {  
                        plus.runtime.quit();  
                    }  
                }, false);  
                compatibleAdjust();  
            }  
            if (window.plus) {  
                plusReady();  
            } else {  
                document.addEventListener('plusready', plusReady, false);  
            }  
             // DOMContentLoaded事件处理  
            var _domReady = false;  
            document.addEventListener('DOMContentLoaded', function() {  
                _domReady = true;  
                compatibleAdjust();  
            }, false);  
             // 兼容性样式调整  
            var _adjust = false;  

            function compatibleAdjust() {  
                    if (_adjust || !window.plus || !_domReady) {  
                        return;  
                    }  
                    _adjust = true;  
                    // iOS平台使用div的滚动条  
                    if ('iOS' == plus.os.name) {  
                        as = 'pop-in';  
                        at = 300;  
                        document.getElementById('content').className = 'scontent';  
                    }  
                    // 预创建二级窗口  
                    preateWebviews();  
                    // 关闭启动界面  
                    setTimeout(function() {  
                        plus.navigator.closeSplashscreen();  
                    }, 500);  
                }  
                // 处理点击事件  
            var _openw = null;  

            function clicked(id) {  
                    if (_openw) {  
                        return;  
                    }  
                    _openw = preate[id];  
                    if (_openw) {  
                        if (_openw.showded) {  
                            _openw.show(as, at);  
                        } else {  
                            _openw.show(as, at);  
                            _openw.showded = true;  
                        }  
                        _openw = null;  
                    } else {  
                        var wa = plus.nativeUI.showWaiting();  
                        _openw = plus.webview.create(id, id, {  
                            scrollIndicator: 'none',  
                            scalable: false,  
                            popGesture: 'hide'  
                        }, {  
                            preate: true  
                        });  
                        preate[id] = _openw;  
                        _openw.addEventListener('loaded', function() { //叶面加载完成后才显示  
                            //      setTimeout(function(){//延后显示可避免低端机上动画时白屏  
                            wa.close();  
                            _openw.show(as, at);  
                            _openw.showded = true;  
                            _openw = null;  
                            //      },200);  
                        }, false);  
                        _openw.addEventListener('close', function() { //页面关闭后可再次打开  
                            _openw = null;  
                            preate[id] && (preate[id] = null); //兼容窗口的关闭  
                        }, false);  
                    }  
                }  
                // 预创建二级页面  
            var preate = {};  

            function preateWebviews() {  
                preateWebivew('plus/webview.html');  
                var plist = document.getElementById('plist').children;  
                // 由于启动是预创建过多Webview窗口会消耗较长的时间,所以这里限制仅创建5个  
                for (var i = 0; i < plist.length && i < 2; i++) {  
                    var id = plist[i].id;  
                    id && (id.length > 0) && preateWebivew(id);  
                }  
            }  

            function preateWebivew(id) {  
                    if (!preate[id]) {  
                        var w = plus.webview.create(id, id, {  
                            scrollIndicator: 'none',  
                            scalable: false,  
                            popGesture: 'hide'  
                        }, {  
                            preate: true  
                        });  
                        preate[id] = w;  
                        w.addEventListener('close', function() { //页面关闭后可再次打开  
                            _openw = null;  
                            preate[id] && (preate[id] = null); //兼容窗口的关闭  
                        }, false);  
                    }  
                }  
                // 清除预创建页面(仅)  

            function preateClear() {  
                for (var p in preate) {  
                    var w = preate[p];  
                    if (w && w.showded && !w.isVisible()) {  
                        w.close();  
                        preate[p] = null;  
                    }  
                }  
            }  
        </script>

第一次点击打开新页面,无等待,返回再点击(第二次以后)就有等待框,这可能是什么原因导致的呢,我所用的代码跟官方提供的都一样的啊(指js代码)

2015-04-15 14:15 负责人:无 分享
已邀请:
DCloud_App_Array

DCloud_App_Array

上面代码有这样的js语句:
w.addEventListener('close', function() { //页面关闭后可再次打开
_openw = null;
preate[id] && (preate[id] = null); //兼容窗口的关闭
}, false);
作用是如果页面关闭了就不再保存窗口对象,避免再次打开时使用无效的Webview对象,而是重新窗口新的Webview窗口对象,从你描述的现象来看就是发生了这种现象。如果要避免这种情况发生,就不要关闭新打开的窗口,而是隐藏即可。
参考HelloH5应用中js/common.js的代码:
在HelloH5中是这么处理的:

// 处理返回事件  
w.back=function(hide){  
    if(w.plus){  
        ws||(ws=plus.webview.currentWebview());  
        if(hide||ws.preate){  
            ws.hide('auto',at);  
        }else{  
            ws.close('auto',at);  
        }  
    }else if(history.length>1){  
        history.back();  
    }else{  
        w.close();  
    }  
};

该问题目前已经被锁定, 无法添加新回复