Link to home
Start Free TrialLog in
Avatar of greddin
greddinFlag for United States of America

asked on

Set ContentType to text/xml with Javascript

Hi: I have the following Javascript function that's working but only writing the data out as a regular webpage. How do I set the content type to "text/xml" so that it displays as a real xml file?

function openRSS() {
      //var html = '';
      var str = '<?xml version=""1.0"" ?>\n';
            str += '<rss version="2.0">\n';
            str += '<channel>\n';
            //str += '<title></title>\n';
            //str += '<link></link>\n';
            str += '<description>CNIC News</description>\n';
            str += '<language>en-us</language>\n';

         var showTextElem = document.getElementById("txtDiv");

         if (showTextElem != null) {
               str += showTextElem.innerHTML;
         }
         else
         {
            alert("Could not find the text section in the HTML!");
          return;
      }

         //html += '\n</bo' + 'dy>\n</ht' + 'ml>';
      str += '</channel>\n';
      str += '</rss>\n';

         var textWin = window.open("","rss");
         textWin.document.open();
         textWin.document.write(str);
         textWin.document.close();
}
Avatar of ClickCentric
ClickCentric

I don't think the content-type can be changed after the fact.  If the file starts off as html, you can't just change it to xml.
ASKER CERTIFIED SOLUTION
Avatar of callrs
callrs

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Set the content type in your original page. Like if you are using JSP then you can set the content type by using page directive as:
<%@ page language="java" contentType="text/xml; charset=UTF-8" %>
You can also replace < with &#62; --> I meant: You can also replace > with &#62;
Or simply use meta tag as:
<meta http-equiv="Content-Type" content="text/xml; charset="utf-8" />
Re: meta tags
Meta tags don't affect the output here in IE 6 (I tried it, no luck)....
Has it ever been determined why you'd want to open a popup with an rss xml feed in it?