开发者

Can I make a link 1. navigate away from page and 2. execute Javascript?

I have a piece of ja开发者_开发知识库vascript code that I want to execute on the page a user navigates to. 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...


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

cross domain scripting

or cross domain ajax etc


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜