Link 打开新页面并运行 javascript
Link that opens a NEW page and runs javascript
我想知道如何打开一个页面,然后在那个页面上,运行javascript(即第一页的link设置)。我试过这个:
<a href = "http://www.example.com/otherpage" onclick = "javascript_on_otherpage()">LINK DISPLAY NAME HERE</a>
有谁知道如何做到这一点?
如果新页面是你控制代码的页面,那么你可以通过link向新页面传递一个查询参数,你可以在新页面检查javascript对于该查询参数,如果找到,运行 页面加载时页面中的一些代码。
<a href = "http://www.example.com/otherpage?runOnStartup=3">LINK DISPLAY NAME HERE</a>
并且新页面中的启动 javascript 将检查 runOnStartup
查询参数和 运行 一些基于其值的代码。
如果新页面与当前页面同源,那么您可以在新页面中打开新页面 window 然后在打开后,您可以在新页面中调用函数 window。但是,您的上一页仍必须 运行ning 才能执行此操作。
如果新页面来自不同的来源,并且您无法控制该新页面中的代码,那么出于浏览器安全原因,您将无法执行您要求的操作。
Warning: Do not use the following code in any remotely-production related machine. It is trivially susceptible to XSS and allows anybody to run any JS code on your website. This means that a malicious website could trivially steal your users' identities and other sensitive data. I highly recommend I recommend against using the method described in this answer in any form/
第 1 页
<a href="mypage.html?javascript=URL_ENCODED_JAVASCRIPT_HERE"></a>
第 2 页
<script>
var javascript = <?=$_GET['javascript'] ?>;
eval(decodeURIComponent(javascript));
</script>
如果您没有 PHP,请使用:
var javascript = window.location.href.match(/[^?=]+$/)[0];
*我还没有测试过这段代码,所以它可能需要一些修复。我还假设您拥有这两个页面。如果目标页面不是您的,出于安全原因,您不能 运行 JavaScript。
URL 编码您的 JavaScript,转到 here,输入您的 JavaScript,然后点击编码。
这是可以的,没问题。
请注意,IE 在 DOM 的这方面存在奇怪的不兼容性。
<!DOCTYPE html>
<html>
<head></head>
<body>
<a id="foo" href>Click me!</a>
<script>
document.getElementById('foo').onclick = onFooClick;
function onFooClick(e) {
var scriptToInject, popUp, node, importedNode;
scriptToInject = 'document.body.innerHTML = "Hello from the injected script!";';
popUp = window.open('about:blank');
node = document.createElement('script');
node.textContent = scriptToInject;
importedNode = popUp.document.importNode(node, true);
popUp.document.body.appendChild(importedNode);
}
</script>
</body>
</html>
以上适用于 Chrome、Firefox 和 Safari Mac。
我想知道如何打开一个页面,然后在那个页面上,运行javascript(即第一页的link设置)。我试过这个:
<a href = "http://www.example.com/otherpage" onclick = "javascript_on_otherpage()">LINK DISPLAY NAME HERE</a>
有谁知道如何做到这一点?
如果新页面是你控制代码的页面,那么你可以通过link向新页面传递一个查询参数,你可以在新页面检查javascript对于该查询参数,如果找到,运行 页面加载时页面中的一些代码。
<a href = "http://www.example.com/otherpage?runOnStartup=3">LINK DISPLAY NAME HERE</a>
并且新页面中的启动 javascript 将检查 runOnStartup
查询参数和 运行 一些基于其值的代码。
如果新页面与当前页面同源,那么您可以在新页面中打开新页面 window 然后在打开后,您可以在新页面中调用函数 window。但是,您的上一页仍必须 运行ning 才能执行此操作。
如果新页面来自不同的来源,并且您无法控制该新页面中的代码,那么出于浏览器安全原因,您将无法执行您要求的操作。
Warning: Do not use the following code in any remotely-production related machine. It is trivially susceptible to XSS and allows anybody to run any JS code on your website. This means that a malicious website could trivially steal your users' identities and other sensitive data. I highly recommend I recommend against using the method described in this answer in any form/
第 1 页
<a href="mypage.html?javascript=URL_ENCODED_JAVASCRIPT_HERE"></a>
第 2 页
<script>
var javascript = <?=$_GET['javascript'] ?>;
eval(decodeURIComponent(javascript));
</script>
如果您没有 PHP,请使用:
var javascript = window.location.href.match(/[^?=]+$/)[0];
*我还没有测试过这段代码,所以它可能需要一些修复。我还假设您拥有这两个页面。如果目标页面不是您的,出于安全原因,您不能 运行 JavaScript。
URL 编码您的 JavaScript,转到 here,输入您的 JavaScript,然后点击编码。
这是可以的,没问题。
请注意,IE 在 DOM 的这方面存在奇怪的不兼容性。
<!DOCTYPE html>
<html>
<head></head>
<body>
<a id="foo" href>Click me!</a>
<script>
document.getElementById('foo').onclick = onFooClick;
function onFooClick(e) {
var scriptToInject, popUp, node, importedNode;
scriptToInject = 'document.body.innerHTML = "Hello from the injected script!";';
popUp = window.open('about:blank');
node = document.createElement('script');
node.textContent = scriptToInject;
importedNode = popUp.document.importNode(node, true);
popUp.document.body.appendChild(importedNode);
}
</script>
</body>
</html>
以上适用于 Chrome、Firefox 和 Safari Mac。