Wednesday, 15 August 2012

javascript - Chrome print blank page -


I have an old javascript code to print pictures, if a user clicks the thumbnail it just works fine There was an empty page in the preview, but recently (only in Chrome!).

Here's a demonstration in Jsbins: Click on the printer icon Now try it in Firefox; It will work as expected.

Chrome: 41.0.2272.89 m

Firefox: 30.0, 36.0.1

  New Window (source) function {win = Window.open ( "", "", "Width = 600, height = 600"); Var doc = win.document; // init head var head = doc.getElementsByTagName ("head") [0]; // title vs title = doc.createElement ("title"); Title.text = "hair window"; Head.appendChild (title); // scripts var code = "function print function () {window.focus (); window.print ();}"; Var script = doc.createElement ("script"); Script.text = code; Script.type = "text / javascript"; Head.appendChild (script); // init body var body = doc.body; // image doc.write ('& lt; img src = "' + src + '" width = "300">'); // Chrome if (Navigator.UzGent.LolverCase). Indexoff ('chrome') & gt; -1) {win.printFunction (); } And {win.document.close (); Win.focus (); Win.print (); Win.close (); }}  

It looks like this & lt; Img & gt; before trying to print; has been loaded, opens the link as a data URI and calls to print within an event handler for the event load window Move, Em> or Blocks , for example

  var code = '\ & lt; Html & gt; \ & Lt; Head & gt; \ & Lt; Title & gt; & Lt; / Title & gt; \ & Lt; Script & gt; \ Function print function () {\ window.focus (); \ Window.print (); \ Window.close (); \} \ Window.addEventListener (\ 'load \', printfunction); \ & Lt; / Script & gt; \ & Lt; / Head & gt; \ & Lt; Body & gt; & Lt; Img src = "'+ src +'" width = "300" & gt; & Lt; / Body & gt; \ & Lt; / Html & gt; '; Window.open ('data: text / html,' + code, '_blank', 'width = 600, height = 600');  

Do not forget code

P> You might on for ; Img & gt; , but if you ever do anything more complex than triggering to print an image you can find it again in the future.

  Doc.write ('& lt; img onload = "printFunction ();" src = "' + src + '" width = "300">);  

Where printfunction is the print function for all browsers


No comments:

Post a Comment