Skip to content

Commit b27a2ee

Browse files
committed
feat(1-3-111.html): add window usage pattern
1 parent 5365a99 commit b27a2ee

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

1-3-111.html

+17-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ <h1>
3434
</aside>
3535
<article>
3636
<h2>1-3-111 window 物件</h2>
37-
<h3 id=""></h3>
37+
<h3>開啟子頁籤</h3>
3838
<p>
3939
現在有一個需求是要開啟子頁籤做其他操作,在母頁籤關閉時子頁籤也要跟著全部關閉以防使用者做出非預期地操作,實作方式如下:
4040
</p>
@@ -87,8 +87,22 @@ <h3 id=""></h3>
8787
// 關閉主畫面時連帶把由主畫面開啟的子畫面一並關閉
8888
window.addEventListener("beforeunload", closeAllSubWindows)
8989
</code></pre>
90-
91-
<div class="border-block"></div>
90+
<h3>複製事件</h3>
91+
<p>
92+
有時候使用者複製一段內容,貼上時會看到貼上結果多了一些空白。這時候如果需要修改複製內容或者禁止複製可以使用下列方法。
93+
</p>
94+
<p>
95+
客戶端如果是第一次使用當前頁面的複製功能,現代瀏覽器會先詢問使用者是否要開啟複製事件的權限。如果網站使用者不同意則以下範例會失效。
96+
</p>
97+
<pre><code class="language-js">
98+
window.addEventListener('copy',async()=>{
99+
let text = '' ;
100+
await navigator.clipboard.readText().then(t=>{
101+
text = t.trim().replaceAll(/\t/g,'');
102+
})
103+
navigator.clipboard.writeText(text);
104+
})
105+
</code></pre>
92106
</article>
93107
</main>
94108
</body>

0 commit comments

Comments
 (0)