Phonegap: InAppBrowser insertCSS file

安稳与你 提交于 2019-12-03 16:58:54

you have to wait until your inAppBrowser page loading finishes.

You must add an event listener:

var inApp = window.open('mypage.html', '_blank', 'location=no');
inApp.addEventListener('loadstop', function(){
    inApp.insertCSS({
        file: 'inAppStyle.css'
    },onSuccess);
});

EDITED

Use this path for your android projects file:///android_asset/{your folder}

INFO: https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md#android-file-system-layout

I couldn't find the right local path. Instead, I just uploaded the css file to the web and provided a regular URL

file: 'http://mywebsite.com/path-if-needed/my.css'

Not ideal to have an external dependency, but not a big deal since InAppBrowser itself requires internet access.

I probably know why it won't work, it is because your path isn't right, this css file should not put in www folder, neither the cordova project folder, u should put it into the server, for example, if ur browser is to visit http://192.168.1.1/admin, then the cordova only fetch this file when the browser is under the 192.168.1.1/admin, it fetch the file under the server directory.I don't know if u use any debug tool , if u use one, it's easy to find out what went wrong, ur console will log the error which path it fetch the css file and didn't get it.

If you want to add an external CSS file stored locally in the APP's sandbox and not around in the Internet, this is the only way, that is, you get the external file, you store it into a string variable, and then you insert such code into the Browser.

var inAppBrowserRef = cordova.InAppBrowser.open(url, "_blank", "location=no");

//when load stops call loadedCallbackFunction
inAppBrowserRef.addEventListener('loadstop', loadedCallbackFunction);

function loadedCallbackFunction() {
    console.log("InAppBrowser Window loaded");

    $.ajax({
        type: "GET",
        url: cordova.file.applicationDirectory + "www/css/myExternalCSS.css",
        dataType: "text",   
        success: function (CSScode) {         

            inAppBrowserRef.insertCSS(
                { code:  JScode},
            function(){
                console.log("CSS code Inserted Succesfully into inApp Browser Window");
            });

        },
        error: function () {
           console.error("Ajax Error");

       }
    });
}

You need the cordova-plugin-inappbrowser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!