Syndicate

Feed

IE7 preliminary bug report: the onclick event-handler

In Internet Explorer 7 (final release, build 7.0.5730.11), we cannot, using Javascript, cancel the browser’s default action for an anchor element by setting this element’s onclick event-handler to false. One way to solve the problem is to remove the href attribute, yet let the cursor that hovers over the element remain pointy (i.e. keep the white hand with the pointing forefinger), using CSS.

The default action of the browser for an anchor element is this: when the “user” clicks on the content of the <a> tag, the browser attempts to retrieve and display the document which URL is indicated by the href attribute, and unless a target attribute says otherwise, the document or fragment is loaded in the same window. We use “return false” to cancel that default action (i.e., the follow link action) in the anchor onclick even-handler function:

myLink.onclick = function() {
  /* some action */
  return false;
}

That “return false” does not work as expected in IE7: the browser still follows the link specified by the href attribute, after it has executed the code that precedes the statement “return false”.

Let us consider the following XHTML markup:

<a id="myLink" href="http://www.11heavens.com">my web site</a>

We must register our event-handlers when the web page is loaded, that is, when the Document Object Model tree is built. In our example, we’ll use a function called registerEventHandlers() — an entirely arbitrary name:

window.onload = registerEventHandlers;

In this function’s definition, we’ll do three things:

  1. First, we’ll add the anchor element to a class. Our goal is to change the aspect of the cursor when it hovers over the anchor element. The CSS rule would be, as defined in our stylesheet:

    .clickable {
      cursor: pointer;
    } /* Rule for the class clickable */

    In registerEventHandlers(), we’ll either set the DOM Level 0 property className, or we’ll go “the long way that works all the time in all modern browsers, and that is W3C-approved:

    var myLink = document.getElementById("myLink");
    var classAttr = myLink.getAttributeNode("class");
    if (classAttr) {
      classAttr.nodeValue += " clickable";
    }
    else { 
      var newAttr = document.createAttribute("class");
      newAttr.nodeValue = "clickable";
      myLink.setAttributeNode(newAttr);
    }
  2. Secondly, we’ll get rid of the href attribute — that is, if we don’t want the browser to follow the link in the usual way after it’s done executing all code assigned to onclick. Since we may need to use the value of this attribute later on, we’ll record it first. Given that the variable url is declared outside the function registerEventHandlers():

    url = myLink.getAttributeNode("href").nodeValue;
    myLink.removeAttributeNode(myLink.getAttributeNode("href"));

    Note that we could have used the method removeAttribute("href") instead ; there are many ways to skin a cat.

    Update : we may remove the attribute node now, or we may remove it later, in the event-handler function.

  3. Finally, we’ll register the onclick event-handler for the anchor element (i.e. the node myLink):

    myLink.onclick = doThis;

That function name, doThis, needs to be more descriptive. What could doThis() do ? For example, it could tell the browser to load in a new window the web page that was referenced by the href attribute we just got rid of:

function doThis() {
  window.open(url, "pop_up");
  /* return false; 
  // This does not prevent the browser’s default action in IE7.*/
}

Update: if we have decided to remove the href attribute node in the event-handler function, we use the keyword “this” to refer to the anchor element:

function doThis() {
  window.open(url, "pop_up");
  this.removeAttributeNode(this.getAttributeNode("href")); 
  /* return false; 
  // This does not prevent the browser’s default action in IE7.*/
}

Please, do not hesitate to comment on this article. Do you have a question? Would you like to propose a correction or improvement? Perhaps you have something to say about IE7? Thank you in advance!

AttachmentSizeHitsLast download
IE7bugSourceCode.zip1.57 KB229 weeks 4 days ago
IE7BugReportOnclickEventHandler.pdf123.47 KB149 weeks 4 days ago
Last edited by Caroline Schnapp about 8 weeks ago.

Because you are not logged in, you cannot see the 2 files attached to this posting. Login or register to download these files.


Comments

Set event.returnValue =

Set event.returnValue = false before the return false, and hey presto, it works in IE7.

Hello there

The returnValue property belongs to an Internet Explorer proprietary Event Object. In plain English, this line of code (this.returnValue = false;) won't be understood by any other browser. It may be a prettier hack than the one I laid out. I will try it, and report back. I tend to stay away from code that is only understood by one browser. For example, I prefer to use only one method to set an attribute, a method that works in all browsers, and for all attributes, including the class and for attributes. I outlined this method above. Thanks for dropping by :) Thanks for the feedback.

href="#"

If you set href="#" then “return false” work as expected in IE7 : the browser will not follow the link specified by the href attribute, after it has executed the code that precedes the statement “return false”.

Yes, that's true. However...

I would rather unset href than set it to '#' because by setting it to '#' the page scrolls up when one clicks on the link. That, for me, is a pain in the arse. It is how I discovered the bug actually. I was trying to understand why using some library (moxie), the page scrolled up whenever I was pressing on 'links'. Same with collapsable fieldsets in forms, if you are using an anchor element (and you shoudn't)... whenever someone opens or closes that part of the form, he loses sight of the form... he's brought to the top of the page!

This used to work

Hey.. this used to work fine in this bug collection application we call IE7, I just installed the latest patch a couple of days ago and got this wonderful behavior.

Just using this: "this.removeAttributeNode(this.getAttributeNode(\'href\')); return false;" in the onclick event of the link worked for me.

I hope they take note of this bug and fix it asap !!!

Thomas

Thomas

I can’t remember why I wasn’t trying to remove the “href” Attr Node in the event-handler function. I will try this asap. Simpler, and logical. Thanks!! [...]

Now, I remember why removing the href Attr Node did not suffice. Once the attribute is removed, the link is still clickable, as it should be, yet does not look that way : when you move your cursor over the link, the cursor remains the same, i.e. it remains a white arrow — or it turns into a text insertion cursor. There’s no longer any indication that we’re dealing with a link. We can certainly get rid of the “href” Attr Node in the event-handler function, but we can also get rid of it at any time. We still have to add the anchor element to a CSS class that will change the aspect of the cursor in its “hover” state, to signal the user that this is a link. If we don’t do that, after a single click we’ll lose that information.

I hope they take note of this bug and fix it asap !!!

I hope so too. I haven’t seen the bug listed anywhere.

why would ie7 have a problem

why would ie7 have a problem with this?? I get the "object doesn't support this property or method." delete_spc_subsection is a function I wrote in JS, that works in FireFox, but throws this random error in ie7.

<a href="javascript:delete_spc_subsection()">delete subsection</a>

thanks in advance

It's sufficient to simply

It's sufficient to simply return false directly in the onclick attribute. So just have this:

<a href="http://www.example.com" onclick="funcName(); return false;">Link Text</a>

For whatever reason, returning false from within funcName() doesn't work.

RE: It's sufficient to simply

onclick="return funcName();"

Onchange event in IE 7.0 not working properly.

The onchange event in IE 7.0 is not working properly, when we write a validation on onchange event then when validation fails and we write 'event.returnvalue=false and event.cancelbubble =true; obj.focus' then this validation runs only once and allows the second time tabbed out. while in ie 6.0 it is working fine.

Couldn't you...

I've not tried this yet - I wanna get it out there...

Couldn't you just set the hRef to javascript:void(0); dodging both the default behavor and the removal of the achor styling.

Having done some research whilst I was writing this - good trick, I know - I just found out I'm not supposed to use javascript:; at all. Well - shoot.

Comments? :S

Weird problem with dropdown onclick event in IE 7.0 for ASP 3.0

Hi, I am facing very weird problem with dropdown onclick event in IE 7.0 for ASP 3.0 application

When page gets load, I fill a dropdown control from the database & while doing so I set the Text of the very 1st option as 'Use Drop Down to select value' (followed by values from the database) Eg: Use Drop Down or 'Find' to locate a skill

I am calling the following function on onClick event of that dropdown & that function (ChangeText) empties the text of the 1st Option (i.e. it removes the 'Use Drop Down or 'Find' to locate a skill' text)

Eg.

function ChangeText(list) { list.options[0].text="" }

After implementing the above logic when user clicks on the dropdown button (to select a value), the data dropdown menu disappears immediately, not allowing user to select any value. The dropdown data menu should stay open and allow user to select a value.

This works absolutely fine in other dropdowns where onClick event has not been implemented.

Also note that there is no such problem in IE 6.0. Donno why its behaving such a way with onClick event in IE 7.0.

I have implemented the above logic in my ASP 3.0 app

Thanx in advance

Regards, Vipul Mehta

Why not just stop the event in its tracks?

function myClickHandler(e) {
    var event = e || window.event;
     if (event.preventDefault) {
         event.preventDefault();
     } else {
         event.returnValue = false;
     }
}

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Allowed HTML tags: <a> <b> <i> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <sup> <sub> <dd> <del> <blockquote> <img> <q> <p> <div>

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
12 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.