1

I have the following script that exports a div content as text:

JAVASCRIPT

  <script type="text/javascript">

      // Wait for the page to load first
        window.onload = function() {

          //Get a reference to the link on the page
          // with an id of "mylink"
          var a = document.getElementById("exportxt");

          //Set code to run when the link is clicked
          // by assigning a function to "onclick"
          a.onclick = function() {

            // Your code here...


function downloadInnerHtml(filename, elId, mimeType) {
    var elHtml = document.getElementById(elId).innerHTML;
    var link = document.createElement('a');
    mimeType = mimeType || 'text/plain';
    link.setAttribute('download', filename);
    link.setAttribute('href', 'data:' + mimeType  +  ';charset=utf-8,' + encodeURIComponent(elHtml));
    link.click(); 
}
var fileName =  'meucanvas.txt'; // You can use the .txt extension if you want
downloadInnerHtml(fileName, 'editor','text/plain');
            //If you don't want the link to actually 
            // redirect the browser to another page,
            // "google.com" in our example here, then
            // return false at the end of this block.
            // Note that this also prevents event bubbling,
            // which is probably what we want here, but won't 
            // always be the case.
            return false;
          }
        }

</script>

HTML

<div style="float:left; padding-left:5px;">
    <a id="exportxt">
    <label>
    <button type="submit" value="#" style="border: 0; background: transparent">
        <b>CLICK HERE DOWNLOAD AS TXT</b>
    </button>
    </label>
    </a>
    </div>




    <div id="editor">
        </br>
    </br>




    TEXT TEXT TEXT

http://jsfiddle.net/JVke4

However, it won't work in Firefox. Works in Chrome. How can I make it cross browser compatible?

Is it the onclick event?

2 Answers2

1

The problem does not have to be window.onload.

Your problem is that you did not add the link element on the page, try this:

function downloadInnerHtml(filename, elId, mimeType) {
    var elHtml = document.getElementById(elId).innerHTML;
    var link = document.createElement('a');
    mimeType = mimeType || 'text/plain';
    link.setAttribute('download', filename);
    link.setAttribute('href', 'data:' + mimeType  +  ';charset=utf-8,' + encodeURIComponent(elHtml));
    link.style.cssText = "position: aboslute !important; left: -9999px; visibility: hidden;";//hide element
    link.innerHTML = "text";
    document.body.appendChild(link);
    link.click(); 
    setTimeout(function(){
        document.body.removeChild(link);//remove element
    }, 1);
}

// Wait for the page to load first
window.onload = function() {

  //Get a reference to the link on the page
  // with an id of "mylink"
  var a = document.getElementById("exportxt");

  //Set code to run when the link is clicked
  // by assigning a function to "onclick"
  a.onclick = function() {

    // Your code here...
    var fileName =  'meucanvas.txt'; // You can use the .txt extension if you want
    downloadInnerHtml(fileName, 'editor','text/plain');
    //If you don't want the link to actually 
    // redirect the browser to another page,
    // "google.com" in our example here, then
    // return false at the end of this block.
    // Note that this also prevents event bubbling,
    // which is probably what we want here, but won't 
    // always be the case.
    return false;
  }
}

Tip:

In some browsers, you may use the attribute download does not work (maybe mobiles) try application/octet-stream, eg.:

function downloadInnerHtml(filename, elId, mimeType) {
    var elHtml = document.getElementById(elId).innerHTML;
    var link = document.createElement('a');
    mimeType = mimeType || 'application/octet-stream';
    link.setAttribute('download', filename);
    link.setAttribute('href', 'data:' + mimeType  +  ';charset=utf-8,' + encodeURIComponent(elHtml));
    link.style.cssText = "position: aboslute !important; left: -9999px; visibility: hidden;";//hide element
    link.innerHTML = "text";
    document.body.appendChild(link);
    link.click(); 
    setTimeout(function(){
        document.body.removeChild(link);//remove element
    }, 1);
}
...
downloadInnerHtml('test.txt', 'editor');//force download
Protomen
  • 9,471
  • 9
  • 57
  • 124
  • Thank you Guilherme Nasimento, it worked now. Best regards from Brazil. –  Mar 07 '14 at 20:22
0

Your problem is in window.load function which is not fired in firefox. Try using JQuery $(document).ready() and other JQuery functions instead (for click etc). JQuery handles those crossbrowser stuff for you.

Check this:

window.onload function doesn't work on Mozilla Firefox

Community
  • 1
  • 1
Hardy
  • 5,590
  • 2
  • 18
  • 27