Search code examples
javascriptinternet-explorerdom-events

IE : get object from event


I want to get href attribute when a use clicks on a URL.

I've tried with:

var e = clickedElement || window.event;
var t = e.target || e.srcElement;
alert(t.href); 

This is working fine in Chrome and Firefox, but there's a problem with IE; its give an error null.

What is possible solution to get href from event object?


Solution

  • Depends what clickedElement is, given that its either the event object argument placeholder or a this argument;

    function event_obj(event) {
        if (!event) var event = window.event;
        var element = event.target || event.srcElement;
        alert(element.href);
        return false;
    }
    function element_obj(element) {
        alert(element.href)
        return false;
    }
    .
    .
    <a href=".." onclick="return event_obj();">foo</a>
    <a href=".." onclick="return element_obj(this);">bar</a>