View Full Version : create a link that runs a js command

08-18-2008, 05:33 AM
ok, im not sure if this is possible. I know that you can run a js command from the address bar, by typing something like javascript:alert('hello');. However, I would like to create a link that can open a webpage and run that js command all together at the same time.

something like:

<a href="http://www.google.com/;javascript:alert('hello');">Click here</a>


<a href="http://www.google.com/&javascript:alert('hello');">Click here</a> except obviously those won't work.

any suggestions are greatly appreciated. thanks.

Philip M
08-18-2008, 09:53 AM
For security reasons JavaScript cannot access anything beyond the domain of the current page. This is known as the "same origin policy" and prevents a document or script loaded from one origin from getting or setting properties of a document from a different origin.

You can do this:-

<a href="#" onclick = "showMessage()" >Click here</a>

<script type = "text/javascript">
function showMessage() {
alert ("Hello");
window.location = "http://www.google.com"

08-18-2008, 06:30 PM
thanks for the reply... bummer though.