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:
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); }
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.
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!
Attachment | Size | Hits | Last download |
---|---|---|---|
IE7bugSourceCode.zip | 1.57 KB | 24 | 13 years 21 weeks ago |
IE7BugReportOnclickEventHandler.pdf | 123.47 KB | 15 | 13 years 32 weeks ago |
Comments
Set event.returnValue =
Set event.returnValue = false before the return false, and hey presto, it works in IE7.
Your website is really cool
Your website is really cool and this is a great inspiring article.
For NRL Tips click here
Thanks for this great post,
Thanks for this great post, i find it very interesting and very well thought out and put together. I look forward to reading your work in the future.
touchartists applications
The default action of the
The default action of the browser for an anchor element is this: when the “user” clicks on the content of the tag, the browser attempts to retrieve elektroniczne papierosy and display e-papierosy the document which URL is indicated by the href attribute, and unless a target attribute says otherwise, the document or fragment is e-papierosy 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:
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 so too. I haven’t seen the bug listed anywhere.
Thanks
Thanks!
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.
delete subsection
thanks in advance
It's sufficient to simply
It's sufficient to simply return false directly in the onclick attribute. So just have this:
Link Text
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.
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;
}
}
Thank You
Thank you for this article - have you learned anything about how this will work in IE8?
I use jQuery
With jQuery, there is no problem.
I assume that when IE8 is out, this bug will be fixed. I have not checked with the current beta.
Problem Resolved!
Thanks so much for posting this article. It saved my bacon. I've been beating my head against this wall for a good part of this last week and thanks to the info I got here, I can now move on to something more productive and enjoyable.
The best solution for me was to insert
if (window.event) event.returnValue = false;
just before the "return false;" statement.
I understand the point you made about being reluctant to use browser specific code such as returnValue, and I agree with that as a general principle. However, I think browser specific code is exactly the right and appropriate response to browser specific bugs.
Cheers...
I think browser specific
Excellent point. If I were to write up a solution today, it would be yours.
Now, I use a JavaScript library so it is not as much relevant to me, but I agree.
This is a great way to
This is a great way to remove the link with javascript from the actual link. I wonder if this works with IE8 now that it's becoming the new mainstream browser because of the automated update windows has. I'm glad that we still get a new better version of the browser anyways that is better with html and css standards.
Another simple solution
javascript:void(0) would do the magic. Void returns "null" with which the browser will just not do anything thereby making our lives a lot better :)
It may be a prettier hack
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. how to get rid of moles on your face
Excellent Post
Twin Fountains EC is a short drive away from Woodlands and Admiralty MRT Station. Future residents will be able to walk to the nearby Vista Point or a short drive to Causeway Point for some family fun and gatherings. A truly unique lifestyle awaits you.
Twin Fountains
this is excellent
Reading this has been a very meaningful experience for me, I will share this great information with others.
twin fountains http://twinfountainsec-woodlands.com
fantastically and awesome information. hope to read moreHedges Park Condo, Topiary EC , Twin Fountains Woodlands
J Gateway is a new and
J Gateway is a new and upcoming condominium located in Boon Lay Way, Jurong East area. It is located right beside JCube, and the upcoming Westgate and Jem. With expected completion in mid 2016, it comprises of 4 towers with 783 units and stands 38 storeys tall.
J Gateway
Coral Edge Residences is
Coral Edge Residences is also near elite schools such as Mee Toh School, Nan Chiau Primary School, Nan Chiau High School.
Many schools are around you.
Coral Edge Residence
DUO Residences is also near
DUO Residences is also near elite schools such as Stamford Primary School and Insworld Institute. St. Margaret Primary School and Singapore Management University (SMU) are also around in the area.
DUO Residence
A wonderful and unique
A wonderful and unique lifestyle awaits you at Woodlands. Please see Forestville EC project details and floor plans for more information.
Forestville EC
I really adore your
I really adore your commitment to offer your readers such valuable information. I am looking forward to read such informative content over and over again. Thanks!
new building by som
visit tp180 here for more info
The Skywoods
The Skywoods is located within an established private housing estate, enjoying good accessibility to major arterial roads and expressway such as Bukit Timah Expressway (BKE), Pan Island Expressway (PIE) and Upper Bukit Timah Road. Travelling to and from the area is also easy with the launch of Hillview MRT Station (Downtown Line, Phase 2) in 2015 which is located within close proximity from The Skywoods, thus making it more accessible to the rest of the island.
Skywood
The Skywood is a lifestyle residential development along Dairy Farm Road and in the Upper Bukit Timah enclave. The Skywoods at Dairy Farm is nestled amongst a lush greenery environment and is set to be the envy of many with the beautiful architecture.
J Gateway
The place to be is J Gateway, the first and possibly the only designated full residential development in the whole of Jurong Gateway – that, together with the lakeside precinct forms the 360ha Jurong Lake District.
Mon Jervois
Mon Jervois, an extremely beautiful show gallery is awaiting for you, nestled in a prestigious and highly sought after residential neighbourhood at Jervois road, an exclusive prime location within close proximity of Good Class Bungalows and various embassies
Ecotech Sunview
Ecotech Sunview, sprawling on more than 300,000 sqft of land and more than 400 Business-2 (B2) units, is well exposed to ample business opportunities with its prime location in the Western region. This industrial development houses units of various configurations to meet an assortment of businesses and spatial demands.
The Tembusu
Residents of The Tembusu will also benefit from the well conceptualized design with both comfort & layout efficiency in mind and every unit will also be furnished with the finest quality fixtures providing you a luxurious yet contemporary home that you deserve.
Thomson Three
Upper Thomson Station, located near Thomson Three, will be part of the $18 billion Thomson Line which is slated for completion in 2020 connecting you to Orchard, Central Business District and Marina Bay area.
Sembawang Drive and
Sembawang Drive and Sembawang Avenue. This location is close to Sembawang MRT and bus interchange. SkyPark Residences is sitting on a good-sized land surrounded by excellent ammenities.stress management
glades condo
The Glades Condo has full and unique facilities, which includes a guard house, clubhouse, children’s playground, swimming pool, kid’s pool, function room, dining pavilion, playground, poolside BBQ, waterjet pool.
The Panorama is a new and
The Panorama is a new and upcoming housing located in the Ang Mo Kio area, nested right in the Ang Mo Kio area. It is within a short drive to Little India, Orchard and city area. With expected completion in mid 2017. The Panorama
The Panorama is a new and
The Panorama is a new and upcoming housing located in the Ang Mo Kio area, nested right in the Ang Mo Kio area. It is within a short drive to Little India, Orchard and city area. With expected completion in mid 2017. The Panorama
Jurong West new Condo Launch by MCL And
Several buses are available near New Condo in Jurong along with shopping centers and restaurants. New Condo in Jurong is also Taman Jurong Shopping Centre as well as Taman Jurong Market and Food Centre. New Condo in Jurong
Alex Residences @ Alexandra View
Alex Residences @ Alexandra View, a district 03 high-rise residential development brought to you by singland. Only a few steps away from redhill mrt, great location with excellent accessibility.. Do not missed this! Visit us @ Alex residences
Boardwalk Residences in Sengkang Fernvale Close
It is situated right beside Layar LRT Station. Future residents will be able to access the nearby Compass Point and Greenwich V which is a short drive away for some family fun and gatherings. A truly unique lifestyle awaits you. fernvale close condo
Boardwalk Residences in Sengkang Fernvale Close
It is situated right beside Layar LRT Station. Future residents will be able to access the nearby Compass Point and Greenwich V which is a short drive away for some family fun and gatherings. A truly unique lifestyle awaits you. Boardwalk Residences in sengkang west
The condo’s facilities
The condo’s facilities provide full family entertainment needs for your family and loved ones. Indulge in a serene and tranquil lifestyle right in the heart of Jurong East. Lakeville Condo Jurong East Condo
Highline Residences has full
Highline Residences has full and unique facilities, which includes a guard house, clubhouse, children’s playground, swimming pool, Aerobic/Yoga room, piano room, pool room, indoor gym, hydrotherapy beds, hydrotherapy baths, reading room, function room, onsen, jacuzzi. http://highlineresidence.net
With expected completion in
With expected completion in mid 2017, it comprises of 8 towers with 597 units and stands 15 storeys tall. Future residents will be able to access the development via the upcoming Tampines West MRT which is just a short drive away. new condo