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 关于#git#的问题,请各位专家解答!(相关搜索:开源项目)
    • ¥15 matlab处理脑电数据悬赏(时序图+预处理+频谱图)
    • ¥100 r语言多元回归模型怎么看表达式
    • ¥20 jenkins+sonarqube实现代码门禁时遇到的问题
    • ¥15 PMM 9010/30P
    • ¥15 pom文件依赖管理,未找到依赖
    • ¥15 现在后端返回给我一堆下载地址,都是一个视频切片后的,如何在uniapp安卓环境下将这些分片的视频下周并且合并成原始视频并下载到本地。
    • ¥15 Unity导出2D项目运行时图片变成马赛克
    • ¥15 关于communitytoolkit.mvvm的生成器得到的代码看起来没有被使用的问题
    • ¥15 matlab中此类型的变量不支持使用点进行索引