Change the content type of a pop up window.

Posted by Oscar Reyes on Stack Overflow See other posts from Stack Overflow or by Oscar Reyes
Published on 2009-09-25T20:32:11Z Indexed on 2010/04/08 16:03 UTC
Read the original article Hit count: 320

Filed under:
|
|
|

This question brought a new one:

I have a html page and I need it to change the content type when the user press "save" button so the browser prompt to save the file to disk

I've been doing this in the server side to offer "excel" versions of the page ( which is basically a html table )

    <c:if test="${page.asExcelAction}">
    <% 
        response.setContentType("application/vnd.ms-excel");
    %>

What I'm trying to do now is to do the same, but in the client side with javacript but I can't manage to do it so.

This is what I've got so far:

<html>
    <head>
        <script>
            function saveAs(){
                var sMarkup =  document.getElementById('content').innerHTML; 
                //var oNewDoc = document.open('application/vnd.ms-excel');        
                var oNewDoc = document.open('text/html');        
                oNewDoc.write( sMarkup );
                oNewDoc.close();
            }
        </script>
    </head>
<body>
<div id='content'>
    <table>
        <tr>
            <td>Stack</td>
            <td>Overflow</td>
        </tr>
    </table>
</div>    
<input type="button" value="Save as" onClick="saveAs()"/>
</body>
</html>

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html