Render XML document (obtained through ajax call) to a new window

前端 未结 5 1111
醉酒成梦
醉酒成梦 2020-12-10 05:57

Hi I\'m looking for a way to render an XML document, that I retrieve using ajax, to a new browser window.

I am using JQuery\'s ajax() function to post JSON data to

5条回答
  •  执念已碎
    2020-12-10 06:28

    The following will work only in FireFox and Opera, but i think is worth mentioning ..

    window.open('data:text/xml,' + encodeURIComponent( jqXHR.responseText ) );
    

    should work with chrome as well but it seems to treat window.open differently than a usual URL.. if you just type the resulting url in chrome it works there as well..


    Update This works with all browsers !

    The thing is that javascript has the ability to tranform xml using xslt.
    But not automatically, so we need to find the XML file for the reference to the XSLT file and load that as well. Then we can do the transformation in javascript and pass the resulting html to the new window.

    Naturally IE handles thing differently than the rest.

    $.get('xml-file-here.xml',
       function(xmlData){
                      var xml = xmlData;
    
                      //extract the stylesheet so we can load it manually
                      var stylesheet;
                       for (var i=0;i

提交回复
热议问题