• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2664
  • Last Modified:

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();
}
0
greddin
Asked:
greddin
  • 3
  • 2
  • 2
1 Solution
 
ClickCentricCommented:
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.
0
 
callrsCommented:
<html>
<script type=text/javascript>
var str='&#60;?xml version=""1.0"" ?&#62;\n'
str += '&#60rss version="2.0">\n';
str += '&#60channel>\n';
//str += '&#60title></title>\n';
//str += '&#60link></link>\n';
str += '&#60description>CNIC News&#60/description>\n';
str += '&#60language>en-us&#60/language>\n';
var textWin = window.open("","rss");
textWin.document.open();
textWin.document.write(str);
textWin.document.close();
</script>
</html>

Save the above text as a new .htm file & open it in your browser.
Solution is:
Replace all < with &#60;
You can also replace < with &#62; but it doesn't seem to make a difference
0
 
gksinghietCommented:
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" %>
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
callrsCommented:
You can also replace < with &#62; --> I meant: You can also replace > with &#62;
0
 
gksinghietCommented:
Or simply use meta tag as:
<meta http-equiv="Content-Type" content="text/xml; charset="utf-8" />
0
 
callrsCommented:
Re: meta tags
Meta tags don't affect the output here in IE 6 (I tried it, no luck)....
0
 
ClickCentricCommented:
Has it ever been determined why you'd want to open a popup with an rss xml feed in it?  
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now