0

JQuery を使用して XML 文字列を作成しています。ユーザーが XML ファイルとして保存できるように、新しいウィンドウを開いてこの文字列をユーザーに表示したいと考えています。

私はサーバー側を持っていないので、この Javascript スクリプトが Firefox および Internet Explorer ブラウザーと互換性があることを望みます。

たくさんのものを見つけましたが、本当にうまくいくものはありません。

uriContent="data:application/xml," + encodeURIComponent(xmlContent);
var newWindow=window.open(uriContent,'_blank','toolbar=0,location=0,directories=0,status=0, scrollbars=1, resizable=1, copyhistory=1, menuBar=1, width=640,height=480, left=50, top=50');

またはIEの場合:

var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(xmlContent);
var newWindow = window.open('','_blank','toolbar=0, location=0, directories=0, status=0, scrollbars=1, resizable=1, copyhistory=1, menuBar=1, width=640, height=480, left=50, top=50', true);
 newWindow.document.writeln(xmlDoc.documentElement.xml);
 newWindow.document.close();

最初の解決策は、Firefox ではほぼ問題なく機能しますが、IE では機能しません。

a data: FF の URL

2 番目のソース コードは、XML コンテンツを含むウィンドウを開きますが、IE はそれを XML として認識しません。したがって、ユーザーは自分でソース コードを表示する必要があります。これはあまり便利ではありません。

空の IE ウィンドウ

誰にも解決策がありますか?

ありがとう!

4

1 に答える 1

2

XML をコンテンツとして扱うとどうなるでしょうか。

var newWindow = window.open('','_blank','toolbar=0, location=0, directories=0, status=0, scrollbars=1, resizable=1, copyhistory=1, menuBar=1, width=640, height=480, left=50, top=50', true);
var preEl = newWindow.document.createElement("pre");
var codeEl = newWindow.document.createElement("code");
codeEl.appendChild(newWindow.document.createTextNode(xmlContent));
preEl.appendChild(codeEl);
newWindow.document.body.appendChild(preEl);

...その後、Google Code PrettifySyntaxHighlighterなどを使用して、必要な強調表示を追加できます。

于 2012-06-13T14:37:44.440 に答える