You can't only use the window.event
to control an event. Try standardizing it like:
function sendDetails(e, type) {
var evt = window.event || e;
if (evt.preventDefault) {
evt.preventDefault();
} else {
evt.returnValue = false;
}
// ...
}
And your HTML would have to be:
<a href="#" class="button" onclick="sendDetails(event, 'Edu');">ASDF</a>
One other non-jQuery solution is to just modify your HTML to be:
<a href="#" class="button" onclick="sendDetails(event, 'Edu'); return false;">ASDF</a>
in which case you wouldn't have to use anything dealing with event
in your sendDetails
function. The return false;
will prevent the default behavior automatically. But note - if any exceptions occur in your sendDetails
function, the return false;
won't execute and will allow the default behavior. That's why I like using preventDefault
- you can call it immediately in the function to immediately stop the behavior, then do what you need.
At the same time, if you're using jQuery, try binding the click event like this:
$(document).ready(function () {
$(".button").on("click", function (e) {
e.preventDefault();
// Your sendDetails code (without the "event" stuff)
// OR call sendDetails (and remove the "event" stuff in the sendDetails function)
});
});
in which case your HTML would be:
<a href="#" class="button">ASDF</a>
Although it would be a lot easier to target the specific elements that this applies to, instead of using the .button
selector I provided. I'm sure the "button" class applies to more than just these targeted <a>
, but maybe I'm wrong :)
Using jQuery is nice in this situation because it already standardizes the event
object in a way that you can just use that e
variable I included in the click
callback. I'm sure it does a little more than just window.event || e
, so I'd prefer/suggest using jQuery for handling events.