実現したいこと
ボタンを押すと textarea に書いたhtmlをポップアップ画面表示したいです。
書き方を教えてください
前提
ボタンを押すと textarea に書いたhtmlをポップアップ画面表示したいです。
発生している問題・エラーメッセージ
エラーメッセージ
該当のソースコード
html
1 2<html> 3<head> 4 <script language="javascript" type="text/javascript"> 5 function PopWindow() { 6 var obj = window.open(); 7 obj.document.open('', '_blank', 'width=50%,height=50%'); 8 obj.document.write(document.getElementById("TextSample").value); 9 obj.document.close(); 10 } 11 </script> 12</head> 13 14<body> 15<input id="submit" type="button" value="ボタン" onclick="return PopWindow()" /> 16 17<textarea style="display:none" name="TextSample" id="TextSample"> 18 <html> 19 <title>テストページ</title> 20 <head> 21 </script> 22 <meta name="viewport" content="width=device-width"> 23 </head> 24 <body> 25 <div id="input-loading-WrireDate" class="input-loading-container "> 26 <input class="form-control" id="WrireDate" name="WrireDate" type="date" value="" /> 27 <span class="visually-hidden">テスト</span> 28 </div> 29 <html> 30</textarea> 31</body> 32</html> 33
試したこと
補足情報(FW/ツールのバージョンなど)
javascript
すでにコードがあるようですが、そのコードで期待通り動かないのでしょうか。
はい。textareaに書いたテストページをポップアップ画面で表示したいのです。
ポップアップ画面で表示するように変える方法がわかりません・・・。
現在はどのような動作になっているのですか?
ボタンを押すとポップアップ画面には白い画面が表示され、
もう一つの画面(textareaに記載したtml)が表示されます。
ありがとうございます。下記にすれば想定通りになりました。
```
<html>
<head>
<script language="javascript" type="text/javascript">
function PopWindow() {
var obj = window.open('', '_blank', 'width=50%,height=50%');
obj.document.write(document.getElementById("TextSample").value);
obj.document.close();
}
</script>
</head>
<body>
<input id="submit" type="button" value="ボタン" onclick="return PopWindow()" />
<textarea style="display:none" name="TextSample" id="TextSample">
<html>
<title>テストページ</title>
<head>
</script>
<meta name="viewport" content="width=device-width">
</head>
<body>
<div id="input-loading-WrireDate" class="input-loading-container ">
<input class="form-control" id="WrireDate" name="WrireDate" type="date" value="" />
<span class="visually-hidden">テスト</span>
</div>
<html>
</textarea>
</body>
</html>
```
window.open() の引数と document.open() の引数を取り違えたのが原因でした。ありがとうございました。
誤り
function PopWindow() {
var obj = window.open();
obj.document.open('', '_blank', 'width=50%,height=50%');
obj.document.write(document.getElementById("TextSample").value);
obj.document.close();
}
↓
修正後
function PopWindow() {
var obj = window.open('', '_blank', 'width=50%,height=50%');
obj.document.write(document.getElementById("TextSample").value);
obj.document.close();
}

回答1件
あなたの回答
tips
プレビュー