Reputation: 10054
So I want to have a link to a site x (which I am not the developer of) that automatically performs some javascript function after it is clicked. EG.
javascript:window.location="http://www.google.com"; alert("Hello");
This performs the alert function before loading the page which is not desired.
Does anyone else know how this can be achieved?
Thanks.
Upvotes: 3
Views: 291
Reputation: 6499
You cannot do that.
Doing this, poses a security threat (XSS), and therefore disallowed by almost all the browsers!!
Worst scenarios to think of, if you had this control:
The Script to capture the username/pass of the user and mail it to you by further calling an Url.
Could play a role in unwarranted tracking/spamming.
EDIT:
This can be done only if user interaction in involved.
For eg:
You can ask user to drag a link to his bookmark toolbar, and the link should contain:
<a href="javascript:alert('click');">Test Click</a>
And then to whatever page the user goes, whenever he clicks the bookmark button (link), an alert happens (or whatever script you may put.)
Upvotes: 0
Reputation: 3725
You can't.
A walk around way is to put the site in to an iframe in your page. Then alert your message in its onload event. And of course your url on address bar will not change.
Upvotes: 0
Reputation: 3524
If you load it in an iframe, I guess you could somehow wait for a certain element to be present and then execute your code.
Upvotes: 1
Reputation: 2961
If the link is changing the "current" windows location, you cant execute your scripts after the external page has started loading.
Upvotes: 0