2012-01-17 48 views
0
<!DOCTYPE HTML> 
<html lang="en-US"> 
<head> 
<meta charset="UTF-8"> 
<title></title> 

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script type="text/javascript"> 

    $(function(){ 

     var xml = '<?xml version="1.0"?><foo><bar>bar</bar></foo>'; 

     document.open("text/xml", "replace"); 
     document.write(xml); 

     document.execCommand('SaveAs',true,'file.xml'); 

    }); 

</script> 

</head> 
<body> 

</body> 
</html> 

該html文件生成一個xml文件(在IE中)並創建一個「另存爲」對話框。但是,我想在保存文件後將文檔重置爲之前的狀態(在「替換」之前)。這可能使用純JavaScript或jQuery?謝謝jquery,保存整個DOM重新加載

回答

1

從開始,針對document運行該代碼,而不是替換當前文檔。

+0

感謝,後續問題:是否有任何的jQuery版本'document.open'?我想'document.write'可以用'append()'代替,我想設置xml的編碼,如果可能的話。 – Johan

+0

編號'document.write'與'append'沒有任何關係。 – SLaks

0

你不需要保存整個dom。只需保存可更改的值(表單字段和其他可溝通的小UI位),並在必要時更新這些值。

如有必要,寫一個復位功能,您可以隨時撥打電話。

+0

我想我可能誤解了這個問題。 – Kristian

+0

顯然:)謝謝 – Johan

0

也許做這一切在一個彈出窗口?

$(函數(){VAR 彈出= window.open() VAR XML = '吧';

popup.document.open("text/xml", "replace"); 
    popup.document.write(xml); 

    popup.document.execCommand('SaveAs',true,'file.xml'); 
    popup.close(); 
});