LoadStop事件触发多次

时间:2019-01-03 05:46:54

标签: cordova cordova-plugins phonegap-plugins phonegap inappbrowser

我正在尝试使用cordova-inapp-browser插件发布HTML页面。我面临的问题是loadstop事件被多次触发。

我试图在buttonclick(onClicked())事件中使用代码,但是post方法根本无法正常工作。我也尝试了loadstart,这也没有给我想要的输出。

var app = {
// Application Constructor
    initialize: function() {
    document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    document.getElementById('btnClick').addEventListener('click',app.onClicked,false);
},

onDeviceReady: function() {
     this.receivedEvent('deviceready');
},

// Update DOM on a Received Event
receivedEvent: function(id) {
    var parentElement = document.getElementById(id);

    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);

},

onClicked: function(){
    var url = "https://httpbin.org/";

    console.log("Clicked Button");
    var target = "_blank";
    var options = "location=no,hidden=yes";
    ref = cordova.InAppBrowser.open('about:blank',target, options);
    ref.addEventListener('loadstop',app.onBrowserLoadStop);
},
onBrowserLoadStop: function(params){

    console.log("Start - Load Stop");

    var options = {
        uid:'ANYUID',
        pwd:'ANYPASSWORD'
    };

    var script = 'var form = document.createElement("form");';
    script += 'var url="https://httpbin.org/post";';
    script += 'form.method="post";';
    script += 'form.setAttribute("action",url);';
    for(var data in options){
        script += 'var hiddenField = document.createElement("input");';
        script += 'hiddenField.setAttribute("type","hidden");';
        script += 'hiddenField.setAttribute("name","'+data+'");';
        script += 'hiddenField.setAttribute("value","'+ options[data] + '");';
        script += 'form.appendChild(hiddenField);';
    }
    script += 'document.body.appendChild(form);';
    script += 'form.submit();';
    console.log("Scirpt: " + script);
    ref.executeScript({
        code:script
    },
    function(values){
        console.log(JSON.stringify("Script: "+ values));
     }
    );
    ref.show();
},
};

app.initialize();

我只想发布一次此http网站。如果您检查控制台,则loadstop事件将继续触发。

1 个答案:

答案 0 :(得分:0)

添加

if (theWebView.isLoading) return;

(void)webViewDidFinishLoad:(UIWebView*)theWebView

CDVUIInAppBrowser.m

插入上面的代码后,当整个页面都加载了所有外部依赖项时,它将仅触发一个“ loadstop”事件。