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"正常工作。我认为浏览器的某些安全规则应该禁止它。
添加回答
举报