Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Show HTML in new window from html string

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.

Aeneas McBurney
Posts: 0
Joined: Mon Jun 16, 2014 7:49 am

Show HTML in new window from html string

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

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Show HTML in new window from html string

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.

Aeneas McBurney
Posts: 0
Joined: Mon Jun 16, 2014 7:49 am

Show HTML in new window from html string

I get both alerts but nothing in window???

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Show HTML in new window from html string

Hi Aeneas,

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

Thanks.

Aeneas McBurney
Posts: 0
Joined: Mon Jun 16, 2014 7:49 am

Show HTML in new window from html string

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

Mangesh Kadam
Posts: 0
Joined: Thu Oct 01, 2015 7:58 pm

Show HTML in new window from html string

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.

Aeneas McBurney
Posts: 0
Joined: Mon Jun 16, 2014 7:49 am

Show HTML in new window from html string

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);
}

Return to “Issues”