Use onClick in Javascript
There are a couple of ways to use onClick in Javascript...
You can write the javascript right into the link tag or you can
call a function located elsewhere in your code.
First, we'll write the code right into the link:
<a href="#" onClick="javascript:window.open('http://google.com');return false">Google</a>
-After the onClick tag, we enclose our entire javascript code in double quotation marks.
-We start with the
javascript: to tell the browser we have javascript code coming up.
-We use javascript's built-in
window.open method to open Google in a new window. Since we are inside double quotes, we use single quotes around our Google address.
-We
return false so the browser won't use the href tag. If we
return true the main browser window would go to the url in the href tag.
Using onClick to call a function is similar and fairly easy:
function goGoogle() {
window.open('http://google.com');
}
<a href="http://yahoo.com" onClick="javascript:goGoogle();return true">Google</a>
-First we
created our function goGoogle.
-Instead of writing our code in the onClick tag, we just called our newly created function.
-This time we returned true so the main browser will go to Yahoo.
Free Copy and Paste Javascript Code:
<script type="text/javascript">
<!--
function goGoogle() {
window.open('http://google.com');
}
-->
</script>
<a href="#" onClick="javascript:window.open('http://google.com');return false">Google 1</a>
<a href="http://yahoo.com" onClick="javascript:goGoogle();return true">Google 2</a>
Resources:
Create a Function in Javascript
Call your Function in Javascript
Opening a REMOTE WINDOW in Javascript
Click HERE to comment or discuss at iLoveTheCode GOOGLE Group