javascriptでwindow.openした時にinputのvalueをクリップボードにコピーできなくて困っています。
わかるかたいらっしゃいましたらお願いします。
javascript
1let filePath = "//xxx/test.xlsx"; 2 3const size = { width: 1280, height: 240 }; 4const position = { y: (screen.availHeight - size.height) / 2, x: (screen.availWidth - size.width) / 2 }; 5const options = "width=" + size.width + ", height=" + size.height + ", top=" + position.y + ", left=" + position.x; 6 7let w = window.open("", "filePath", options); 8w.document.body.innerHTML = ""; 9 10w.document.write( 11 "<input type=\"text\" id=\"copyTarget\" style=\"height: 26px; width: 800px\" value=\"" + filePath + "\" readonly />" 12 + "<button style=\"margin-left: 5px\" onclick=\"" 13 + function clipboardCopy() { 14 if (document.getElementById("copyTarget")) { 15 document.getElementById("copyTarget").select(); 16 document.execCommand("Copy"); 17 } 18 } 19 + "\">クリップボードにコピー</button>" 20);
サーバー上のエクセルデータを開くのでしょうか?
エクセルデータにdocumentがあるという認識でしょうか?
document.writeの使用はマストでしょうか?
そもそも何をやりたいかわかりません
コメントありがとうございます。サーバーのファイルパスをコピーしたいだけになります。
別windowで開いてコピーをしたいですが、別windowを開く前にボタンを押さずにクリップボードにコピーでもいいです。
調べてみましたが、navigator.clipboard.writeText("test"); が使えるかもしれません。
let filePath = "//xxx/test.xlsx";の「//xxx/test.xlsx」をクリップボードにコピーするのですか?
windowは本当に開かなくていいんですね?
はい、クリップボードのコピーができればいいですが、
navigator.clipboard.writeText("test"); がGoogle Chromeで動きませんでした。
httpのサイトでは動かないようです。
社内システムのサーバー都合上、httpsが使えないので何か方法があれば。
document.execCommand('copy')は非推奨のようですが。
回答2件
あなたの回答
tips
プレビュー