weixin_33736649 2015-10-19 12:00 采纳率: 0%
浏览 33

将网页另存为HTML错误

I am using this https://github.com/eligrey/FileSaver.js to save web page as html.

Following code executes when user clicks download-

var originalstr=$.ajax(
{
    type: "GET",         
    url:"url",
    async: false 

}).responseText  ;
var  str = $(originalstr).find('.toolbar').remove().end();
saveAs(new Blob([ str], {type: "text/html;charset=utf-8"}), "Plan_Enrollment_Show_All.HTML");

I get the response and i am removing a div tag with class name "toolbar" and then i am sending it to saveAs function of the FileSaver.js.

I am getting the web page with "[object Object]" as the content file which got downloaded.

If i don't do any modification and pass the originalstr then i get the proper output. Any help regarding this?

I have taken the escaped html and tried constructing the html by removing the div and it worked. For escaping the html i used this http://www.freeformatter.com/javascript-escape.html#ad-output but when i try to do escaping using escape() function of javascript i get different response.

Demo of removing the tag with class- https://jsfiddle.net/SkyTreasure/zfzocuf2/

  • 写回答

1条回答 默认 最新

  • csdnceshi62 2015-10-19 14:04
    关注

    As far as I understood, you needed data/table even after manipulation of the $s variable. I guess as pointed out, you have to say which content has to be displayed. In this case, "div or table" both will give the same result. But, one other good thing is to clone the HTML content(outerHTML function).

    $.fn.outerHTML = function() {
      return $('<div />').append(this.eq(0).clone()).html();
    };
    
    var $s = $(s).find('.a-IRR-toolbar').remove().end();
    console.log($s);
    
    var $t = $(s).find('.a-IRR-toolbar').remove().end().find("div").outerHTML();
    console.log($t);
    

    Check this JSFiddle

    Another thing, $t works in most conditions-

    var $t = $(s).find('.a-IRR-toolbar').remove().end().find("div");
    
    var $t = $(s).find('.a-IRR-toolbar').remove().end().find("table");
    
    var $t = $(s).find('.a-IRR-toolbar').remove().end().find("div").outerHTML();
    
    var $t = $(s).find('.a-IRR-toolbar').remove().end().find("div").html();
    

    All the above values give the same result. But, what is implemented is a good practice.

    Let me know if anything specific is needed.

    评论

报告相同问题?