-1

I have an HTML Document object assigned to variable var doc. Using this document object i am rendering string values to a text file where values are getting rendered and written but in an improper format in IE11 browser but working fine in IE8,IE10,ff n chrome.Please find my below code:

  function savecontent(str){
  var filename = "data.txt";
  var str=replaceAll(str,'<doublequote>','"');
  var w = window.frames.w;
  if( !w )
     {
             w = document.createElement( 'iframe' );
             w.id = 'w';
             w.style.display = 'none';
             document.body.insertBefore( w,null );
             w = window.frames.w;
             if( !w )
             {
                     w = window.open( '', '_temp', 'width=100,height=100' );
                     if( !w )
                     {
                             window.alert( 'Sorry, could not create file.' ); return false;
                     }
             }
     }

  var doc = w.document;
  doc.open('text/plain');
  doc.charset="iso-8859-1";
  doc.write(str);
  doc.close(doc.write(str));
  w.close();
  if( doc.execCommand( 'SaveAs', false, filename ) )
     {
             window.alert("Please save the file.");
     }
}

Where my str could be something like employee_firstname,employee_lastname,employee_id,employee_salary,employee accountno,employee_dob etc..

which is rendered in IE11 as,

employee_firstname,employee_lastname,
employee_id,employee_salary,employee accountno,employee_dob

but where as expected is and data is rendered in IE8,ff n chrome in the below format:

employee_firstname,employee_lastname,employee_id,
employee_salary,employee accountno,employee_dob

The different i notice in other browser like IE8,FF n chrome is line break happening differently in IE11 compared to other browsers. Can anyone please tell me how to format rendering of data properly in text file in IE11 browser or any alternative to document.write()?

kss
  • 3
  • 4
  • 1) Show complete code that reproduces the issue. 2) Explain what the difference is—it appears to be just the location of a line break, is this real? 3) Note that the fragments of code don’t work for various reasons, like using `window` as a local variable and trying to set `charset`. – Jukka K. Korpela Jan 22 '15 at 08:07
  • You have modified the code, avoiding one of the problems mentioned, but it’s still just a function. There’s even no function call, and no HTML. – Jukka K. Korpela Jan 22 '15 at 08:25
  • This above function is javascript function which called onclick of a button and string is passed to the function. – kss Jan 22 '15 at 08:54
  • Please provide actual code by editing the question. I think I am starting to see what you mean and what happens, but this depends on the exact code, including the real data and the width of the frame. – Jukka K. Korpela Jan 22 '15 at 09:03
  • JukkaK.Korpela posted the complete code – kss Jan 22 '15 at 09:20

1 Answers1

1

The problem cannot quite be reconstructed from the code supplied, but the heart of the problem seems to be that you are generating a new document to be displayed in an inline frame, using the open() method for a Document object. This is relatively well supported, but only when the created document is an HTML document, not a plain text document.

When you try to use text/plain format, browsers handle things differently. They actually create an HTML document, placed in the DOM tree of the creating document. It contains a body part that either has just the text you have written or a pre element wrapper around it, causing it to be displayed as-is. For example, old versions of IE generate the pre element, IE 11 does not. It might be argued that IE 11 does the right thing: being plain text does not mean that text should be rendered as-is with respect to division into lines.

Anyway, the way to avoid this is to generate an HTML document and insert the pre wrapper with your code, provided that you wish to display the text as-is:

doc.open('text/html');
doc.write('<pre>' + str + '</pre>');
Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390