[英]Can I make a link 1. navigate away from page and 2. execute Javascript?
I have a piece of javascript code that I want to execute on the page a user navigates to. 我要在用户导航到的页面上执行一段JavaScript代码。 So I have 所以我有
<a href="www.google.com">
and once he clicks it I want to execute 当他单击它后,我要执行
javascript:Alert("you're on google")
Is there anyway to do this? 反正有这样做吗?
I thought of setting up a PHP page somewhere that accepts a URL as a parameter and then wraps the js in it, but that would affect my webserver traffic and probably not work for session based websites... 我想到在某个地方设置一个PHP页面,该页面接受URL作为参数,然后将js包装在其中,但这会影响我的网络服务器流量,并且可能不适用于基于会话的网站...
You can for sure execute a script * then * navigate away . 您可以确定执行脚本 * 然后 * 离开 。 But if you want to execute your script on the next page, in short , it is not possible (or all browsers try their best to make it not possible). 但是, 简而言之 ,如果您想在下一页执行脚本,则不可能(或者所有浏览器都尽力使之不可能)。 This is related to cross-page security. 这与跨页面安全性有关。
<a onclick="yourscript()" href="http://www.google.com">go to google</a>
If you are thinking about cross-domain scripting, you can google for 如果您正在考虑跨域脚本编写,则可以通过Google搜索
cross domain scripting 跨域脚本
or cross domain ajax etc 或跨域ajax等
Thankfully, no. 幸运的是,没有。 Or, at least, you are not supposed to be able to do that. 或者,至少,您不应该能够做到这一点。 Running your javascript on someone else's web site is a gigantic security hole. 在其他人的网站上运行您的JavaScript是一个巨大的安全漏洞。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.