making a link trigger javascript

Solutions on MaxInterview for making a link trigger javascript by the best coders in the world

showing results for - "making a link trigger javascript"
Jessica
12 Apr 2016
1<html>
2<head>
3    <script type="text/javascript">
4
5        // Wait for the page to load first
6        window.onload = function() {
7
8          //Get a reference to the link on the page
9          // with an id of "mylink"
10          var a = document.getElementById("mylink");
11
12          //Set code to run when the link is clicked
13          // by assigning a function to "onclick"
14          a.onclick = function() {
15
16            // Your code here...
17
18            //If you don't want the link to actually 
19            // redirect the browser to another page,
20            // "google.com" in our example here, then
21            // return false at the end of this block.
22            // Note that this also prevents event bubbling,
23            // which is probably what we want here, but won't 
24            // always be the case.
25            return false;
26          }
27        }
28    </script>
29</head>
30<body>
31    <a id="mylink" href="http://www.google.com">linky</a>        
32</body>
33</html>