js function...

Mike Eghtebas
Mike Eghtebas used Ask the Experts™
I have this html hooked to the js file that follows:

            <title>Welcome to our site</title>
            <script type="text/javascript" src="smarterlinks.js"></script>

            <h2 align="center">
                  <a href="JS_3.html" id="redirect"> Welcom to our site 2...</a>


function initAll() {
      document.getElementById("redirect").onclick = false; //clickhandler;
/*function clickhandler() {
      return false;
      //return true;

Instead of calling clickhandler, I want to hard code the line in initAll() as true or false.

Question: Why this doesn't work when I change:

document.getElementById("redirect").onclick = clickhandler;   to
document.getElementById("redirect").onclick = false;   or
document.getElementById("redirect").onclick = true;

I an trying to understand working of function in js.

regardless of whether I have:
document.getElementById("redirect").onclick = false;
document.getElementById("redirect").onclick = true;

it fallows the link. With = false; I expect not to follow the link.

Thank you.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
its because, the "onclick" expects a function (handler function) and not a value.
The browser checks for the handler function for the click event and not a value. Hence if you assign a value it will ignore and default action or handler function will be executed.

try outputing using
in the initAll() function after assigning value and after assigning handler function. You will see the difference.
document.getElementById("redirect").onclick=return false;

Open in new window

Kusala WijayasenaSoftware Engineer
try this

window.onload = function(){
	 document.getElementById("redirect").onclick = function(){
		return false

Open in new window

Here onClick is an event and not a property like checked or visible etc. Normally no matter what language when a event is invoked it expects some action. These actions are provided by functions or handlers or listeners. YOu could have notices when wirting a inline event call inside a element there will be a fuction called or a return statements. like

<input type="test" ... onclick="document.,,,display='none';return false;"

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial