【问题标题】:how to show an error when in appbrowser does not load error phonegap在appbrowser中如何显示错误不加载错误phonegap
【发布时间】:2017-12-17 12:06:37
【问题描述】:

我正在开发一个应用程序并使用 inapp 浏览器插件 cordova-plugin-inappbrowser 加载托管应用程序

我已经获得了大部分功能,但是当他的 url 没有加载时,我无法获得加载错误消息的部分,它只是工作并显示我托管我的 url 的错误消息应用程序。

这可能很尴尬。

请帮我解决这个问题

这就是下面的工作,感谢您的潜在回应

    // my child browser code, the main source of my app content

function fire(){

  var ref = cordova.InAppBrowser.open('http://####################', '_blank', 'location=no,zoom=no,hardwareback=yes,clearsessioncache=yes,clearcache=no');

var myCallback = function(event) { alert(event.url); }

ref.addEventListener('loadstart', inAppBrowserbLoadStart);

ref.addEventListener('loadstop', inAppBrowserbLoadStop);

ref.addEventListener('loaderror', loadErrorCallBack);

ref.addEventListener('exit', inAppBrowserbClose);

}

function loadErrorCallBack(params) {



    $('#status-message').text("");



    var scriptErrorMesssage =

       "alert('Sorry we cannot open that page. Message from the server is : "

       + params.message + "');"



    inAppBrowserRef.executeScript({ code: scriptErrorMesssage }, executeScriptCallBack);



    inAppBrowserRef.close();



    inAppBrowserRef = undefined;



}



function executeScriptCallBack(params) {





    if (params[0] == null) {





        $('#status-message').text(

           "Sorry we couldn't open that page. Message from the server is : '"

           + params.message + "'");

    }





}

【问题讨论】:

    标签: javascript phonegap inappbrowser


    【解决方案1】:

    您的代码通常没问题,但您无法控制alert() 函数的标题。您可以使用其他一些技术来显示错误消息。例如,您可以使用div

    function loadErrorCallBack(params) {
      $('#status-message').text("");
      var scriptErrorMesssage = createMsg('Sorry we cannot open that page. Message from the server is: '
                              + params.message);
      inAppBrowserRef.executeScript({
        code: scriptErrorMesssage
      }, executeScriptCallBack);
      inAppBrowserRef.close();
      inAppBrowserRef = undefined;
    }
    
    function createMsg(msg) {
      return 'var div = document.createElement("div");'
           + 'div.style.position = "absolute";'
           + 'div.style.top = "50%";'
           + 'div.style.left = "50%";'
           + 'div.style.width = "100px";'
           + 'div.style.height = "100px";'
           + 'div.style.color = "#f00";'
           + 'div.innerHTML = "' + msg + '";'
           + 'document.appendChild(div);'
    }
    

    【讨论】:

    • 谢谢,按说明更新错误并回复您,非常感谢
    • 嗨,刚刚尝试了您的更新,但它仍然相同,在我编译了我的应用程序并再次测试之后,你能否给我一个关于你如何实现它的基本用法,以便我可以编译和测试,非常感谢坦克
    • 如果您仍然看到包含项目 URL 的错误消息,这意味着您有另一个alert(),而不是您的问题中的那个。搜索它并将其注释掉。或者您有一个您没有处理的异常(即放在try...catch 之间),在这种情况下,您必须调试您的应用程序以查看发生了什么。
    • 嗨,你可以看看我的完整 html 代码paste.ofcode.org/gEaUeMfvRAG5b3MKcYcgq6
    • var myCallback = function(event) { alert(event.url); } 行末尾需要一个分号。你可以删除它,因为它没有被使用。在 deviceready 事件之前不能调用 inAppBrowser。您在 8 秒后调用它,这可能不够,也可能不够,所以请改为在 deviceready 中调用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 2015-12-18
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多