链接可打开一个新页面并运行javascript [英] Link that opens a NEW page and runs javascript

查看:108
本文介绍了链接可打开一个新页面并运行javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何打开一个页面,然后在该页面上运行javascript(由第一页上的链接设置).我已经试过了: <a href = "http://www.example.com/otherpage" onclick = "javascript_on_otherpage()">LINK DISPLAY NAME HERE</a> 有人知道如何实现这一目标吗?

I would like to know how to open a page, and then on that page, run javascript (that is set by the link on the first page). I have tried this: <a href = "http://www.example.com/otherpage" onclick = "javascript_on_otherpage()">LINK DISPLAY NAME HERE</a> Does anyone know how to achieve this?

推荐答案

警告:请勿在任何与远程生产相关的计算机上使用以下代码.它很容易受到XSS的影响,并允许任何人在您的网站上运行任何JS代码.这意味着恶意网站可能会轻易窃取用户的身份和其他敏感数据.我强烈建议不要以任何形式/

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,请使用:

If you don't have PHP, use:

var javascript = window.location.href.match(/[^?=]+$/)[0];

*我尚未测试此代码,因此可能需要修复.我还假设您同时拥有这两个页面.如果目标页面不是您的,则出于安全原因,您无法在其上运行JavaScript.

*I haven't tested this code so it may need some fixing. I am also assuming you own both pages. If the target page isn't yours, you can't run JavaScript on it for security reasons.

要对您的JavaScript进行URL编码,请在此处,键入您的JavaScript,然后点击编码"

to URL encode your JavaScript, go here, type your JavaScript, and hit encode.

这篇关于链接可打开一个新页面并运行javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆