Page 2 of 2

Show HTML in new window from html string

Posted: Wed Apr 15, 2015 2:45 am
by Yurii Orishchuk

Hi Aeneas,

q1:

pre

Will this code still work when running code from browser or do I need to check first and change back to other way?

/pre

Unfortunatly nope. You need to use separate solutions for web app and native app.

Here is a little hint about condition:

pre

var wnd = window.open("about:blank","_blank","directories=no,status=no,menubar=no,scrollbars=no,resizable=no,EnableViewPortScale=yes");

if(wnd.document){
//here code for browser
}
else{
//here code for inappbrowser
}

/pre

q2:

can not tell you about your code need to test.. So please use give above code:

precode

var invhtml = '<div style="background: red;">hello world<>';
var wnd = window&#46;open("about:blank","_blank","directories=no,status=no,menubar=no,scrollbars=no,resizable=no,EnableViewPortScale=yes");
wnd&#46;addEventListener( "loadstop", function() {
wnd&#46;executeScript({ code: 'document&#46;write(' + invhtml + ');' });
});

/code/pre

After you will get this code to work - you can change "invhtml" variable with your HTML data.

Regards.


Show HTML in new window from html string

Posted: Wed Apr 15, 2015 6:40 am
by Aeneas McBurney

Here is my code

invhtml = 'hello world';

var wnd = window.open("about:blank","_blank","directories=no,status=no,menubar=no,scrollbars=no,resizable=no,EnableViewPortScale=yes");
if(wnd.document){
wnd.document.write(invhtml);
}
else{
wnd.addEventListener("loadstop", function() {
wnd.executeScript({ code: 'document.write(' + invhtml + ');' });
});
}

Browser Result:

Image

InApp Result:
Image


Show HTML in new window from html string

Posted: Thu Apr 16, 2015 3:37 am
by Yurii Orishchuk

HI Aeneas,

Tried your code and it works in my device.

precode

var invhtml = '<div style="background: red;">hello world<>';

var wnd = window&#46;open("about:blank","_blank","directories=no,status=no,menubar=no,scrollbars=no,resizable=no,EnableViewPortScale=yes");

if(wnd&#46;document){
wnd&#46;document&#46;write(invhtml);
}
else{
wnd&#46;addEventListener("loadstop", function() {
alert("Before write");
wnd&#46;executeScript({ code: 'document&#46;write(/code' + invhtml + ');' });
alert("after write");
});
}

/pre

Please make sure you have alerts in your device.

Regards.


Show HTML in new window from html string

Posted: Thu Apr 16, 2015 4:28 am
by Aeneas McBurney

I get both alerts but nothing in window???


Show HTML in new window from html string

Posted: Mon Apr 20, 2015 1:21 am
by Yurii Orishchuk

Hi Aeneas,

Please share your app with support and tell us steps to reproduce this issue in your app.

Thanks.


Show HTML in new window from html string

Posted: Mon Apr 20, 2015 1:23 am
by Aeneas McBurney

HI Yurri,

Thanks for your reply but I ended up writing the html file to the device and opening from there which works. I am only writing from a string when in browser mode.

Thanks,
Aeneas


Show HTML in new window from html string

Posted: Thu Oct 01, 2015 7:58 pm
by Mangesh Kadam

Hi, Can you please share of code of writing the html file as I'm getting blank screen as well.
I have html string like -
var HtmlString = '< html < body .... < / body < / html '
Which seems showing blank screen in InAppBrowser.
Thanks in advance.


Show HTML in new window from html string

Posted: Sun Oct 04, 2015 8:50 pm
by Aeneas McBurney

I write the html to a localStorage variable and this is the code I use which will either write to a window (if web based) or a file if device based

if(plat!="iOS" && plat!="Android"){
var wnd = window.open("about:blank","_blank","directories=no,status=no,menubar=no,scrollbars=no,resizable=no,EnableViewPortScale=yes");
wnd.document.write(localStorage.getItem("invoiceHTML"));
}
else{
// Second, we gonna check or create a new file in a specific folder
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSWin2, onFSFail2);

}

function onFSFail2(error) {
console.log(error.code);
}

function gotFileWriter(writer) {

writer.onwriteend = function(evt) {
console.log("contents of file now 'some sample text'");

Code: Select all

//writer.truncate(11); 

writer.onwriteend = function(evt) { 
    console.log("contents of file now 'some sample'"); 

    //writer.seek(4); 
    //writer.write(" different text"); 

    writer.onwriteend = function(evt){ 
        console.log("contents of file now 'some different text'"); 
    }; 
}; 

};

Code: Select all

 writer.write(localStorage.getItem("invoiceHTML")); 
 //alert('open'); 
 window.open(localStorage.getItem("localStorageDirectory")  +invfilename, '_blank', 'directories=no,status=no,menubar=no,scrollbars=no,resizable=no,EnableViewPortScale=yes');  
 localStorage.setItem("invoiceLocalFile",invfilename); 

}

function onFSWin2(fileSystem) {

Code: Select all

 //create new file 
 //alert('create'); 
 fileSystem.root.getFile("tradieTrack/" +invfilename, {create: true, exclusive: false}, gotFileEntry, onFSFail2); 

}

function gotFileEntry(fileEntry) {
console.log("New file: " + fileEntry.fullPath);
//alert('newfile');
//fileEntry.remove(successCallback, opt_errorCallback);
fileEntry.createWriter(gotFileWriter, onFSFail2);
}

function onFSWin(fileSystem) {
fileSystem.root.getDirectory("tradieTrack", {create: true, exclusive: false}, getDirectorySuccess, getDirectoryFail);
}

function getDirectorySuccess(parent) {
console.log("New Folder or Folder already exists: " + parent.fullPath);
}

function getDirectoryFail(error) {
console.log("Unable to create new directory: " + error.code);
}