irrumate Posted November 4, 2008 Share Posted November 4, 2008 Hello,I was wondering if there was a way to take a simple onClick command such as:<HTML><HEAD><TITLE>OnClick alert message</TITLE><script LANGUAGE="JavaScript">function pushbutton() { alert("Here is where you write what you want it to say");}</SCRIPT></HEAD><BODY><CENTER><A HREF="" onclick="pushbutton();">Click Here!</A></CENTER></BODY></HTML>and be able to make it work for all the links on the same page so I don't have to keep using the "a hef="" onclick="pushbutton();"" command. I am going to have a bunch of links on a page and want the same alert box message to appear when any of the links are clicked. Please leave your input. Thanks Link to comment Share on other sites More sharing options...
Synook Posted November 4, 2008 Share Posted November 4, 2008 You can attach the event onload to all links, as such: window.onload = function() { var links = document.getElementsByTagName("a"); foreach (i in links) { links[i].onclick = pushbutton; //note: no () }} By the way, it is best to put for the href attribute "java script:void(0)" so that the browser doesn't do anything besides run the JS. Link to comment Share on other sites More sharing options...
Jesdisciple Posted November 4, 2008 Share Posted November 4, 2008 By the way, it is best to put for the href attribute "java script:void(0)" so that the browser doesn't do anything besides run the JS.I don't understand why people still use void... Wouldn't javascript: return false; do the same thing? Link to comment Share on other sites More sharing options...
Synook Posted November 5, 2008 Share Posted November 5, 2008 "return false" is 12 chars long, while "void(0)" is only 7 chars long Link to comment Share on other sites More sharing options...
irrumate Posted November 6, 2008 Author Share Posted November 6, 2008 You can attach the event onload to all links, as such:window.onload = function() { var links = document.getElementsByTagName("a"); foreach (i in links) { links[i].onclick = pushbutton; //note: no () }} By the way, it is best to put for the href attribute "java script:void(0)" so that the browser doesn't do anything besides run the JS. How would I go about putting this into my script? (i.e.- what would the full code look like?) Link to comment Share on other sites More sharing options...
Jesdisciple Posted November 6, 2008 Share Posted November 6, 2008 See the first item in my signature for that href. <HTML> <HEAD> <TITLE>OnClick alert message</TITLE> <script LANGUAGE="JavaScript"> window.onload = function() { var links = document.getElementsByTagName("a"); foreach (i in links) { links[i].onclick = pushbutton; //note: no () } } function pushbutton() { alert("Here is where you write what you want it to say"); } </SCRIPT> </HEAD> <BODY> <CENTER> <A HREF="java script:void(0)" onclick="pushbutton();">Click Here!</A> </CENTER> </BODY></HTML> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.