次の2つのJS関数を使用して、次の手順を実行します。ユーザー操作によって生成されたiFramehtmlコンテンツを文字列に取得します。次に「myStylesheet.css」という名前のサーバーに含まれるCSSスタイルシートを文字列に取得します。
var iframe = document.getElementById('frame');
function downloadFile(filename, text) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
if (document.createEvent) {
var event = document.createEvent('MouseEvents');
event.initEvent('click', true, true);
pom.dispatchEvent(event);
}
else {
pom.click();
}
//example : downloadFile("hello.txt", "Hello World!") or for custom name filedownloadFile(myPageName +'.html', iFrameContent)
}
function publishWebsite(){
var htmlCodeForFrame = document.getElementById('frame').contentWindow.document.body.innerHTML;
var ccsCodeForFrame = "";
downloadFile("myWebsite.html", htmlCodeForFrame); //save html of frame into a file and download it to user
downloadFile("myStylesheet.css", ccsCodeForFrame); //save css of frame into a file and download it to user
}
私の問題は、CSSスタイルシートを変数に変換してからユーザーにダウンロードすることです(これはチームプロジェクトであり、他の多くの場所で使用されているため、わかりやすくするためにダウンロード機能を使い続けたいと思います)。これをJavascriptで実行したいと思います。
少しいじった後、私は以下を使用していることに気づきました。
<iframe id="cssFrame" src="myStylesheet.css" style="display: none;"></iframe>
最初の投稿のコードを組み合わせて、iframeのコンテンツを文字列に保存し、その文字列を.css拡張子の付いたファイルに保存しました。うまく機能します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加