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.

    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵