1 回答

TA貢獻1866條經驗 獲得超5個贊
這應該適用于幾乎所有的瀏覽器(在 FF 和 Chrome 中測試):
<!-- Your link !-->
<a id="externalLink" href="#/">Click to run the code</a>
<script>
const el = document.getElementById('externalLink');
// Create a content for a new page with a script in it
const newPageContent = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>alert('Hello World!')<\/script>
Hello world!
</body>
</html>
`;
// Listen on "click" event
el.addEventListener('click', (e) => {
e.preventDefault();
// Create a new window on click
const w = window.open('', '_blank');
// Write your content to it
w.document.write(newPageContent);
});
</script>
雖然我無法href="data:text/html"正常工作。我認為瀏覽器的某些安全規則應該禁止它。
添加回答
舉報