htmlEle.fireEvent("onclick", document.createEventObject()) doesn't work on IE 11 - javascript

Hi I have a click event associated with a image button. I am trying to fire the click event by htmlEle.fireEvent("onclick", document.createEventObject()) but i get error object doesn't support property or method 'createEventObject' on IE 11. The click event works perfectly fine for IE 9. Is there a substitute for createEventObject that i can use for IE 11? Thanks

I think createEventObject is not supported in IE 10+ (source).
Try using something like this:
var evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
myObject.target.dispatchEvent(evt);
as explained here.

Related

preventDefault() not working as desired

<script>
function confirmDel(evt)
{var con =false;
con=confirm('Do you really want to remove this purchase?.');
if(con)
{
return true;
}else
{
event.preventDefault();
}
}
</script>
my html
<a title="View Log" onclick="return confirmDel(this);" href="index.php?mod=tech_support">delete</a>
above code works fine in chrome browser but fails in Mozilla .
but when i use return false instead of event.preventDefault(); it works fine in both.
can anyone explain why this happens
The problem is with event.preventDefault(). The event you're passing is evt, not event. You have to use:
evt.preventDefault();
event.preventDefault() works in Chrome because it mimics old IE behavior for backwards compatibility (just like it also has innerText).
In old IE, the event object was window.event. So calling event.preventDefault() calls the global event object, which works in Chrome and IE, but not Firefox which doesn't implement this non-standard behavior.

Jquery load not working on internet explorer after then ready function

In firefox or chrome or other browsers i dont have this problem but in ie not working.
$(document).ready(function(){
$('#divplayer').load("/player/index.php");
$("a.eklebeni").live('click', function(event){
event.preventDefault();
$('.png', this).attr('src', 'img/eklendi.png');
$.get($(this).attr("href"), function(data) {
$('#divplayer').html('<img src="img/player.png" class="png" alt="" />');
$('#divplayer').load("/player/index.php");
});
});
I dont know why but load function not working ?
Thanks
Neither of these a jQuery related solutions... but they're both things you should be aware of in the difference between IE and W3C (everyone else). It looks like you might have coded for the latter which is why in IE it's not working as you expect, but without knowing what's not working this is a big of a WAG.
In IE, window.event gives you access to the event (not the first argument passed to the event handling code) so your function should be:
//...
.live('click',function(event) {
event=event||window.event;
//...
To prevent an event in W3C you should use event.preventDefault(), in IE you need to set event.returnValue=false in order to prevent the default event. Which means changing a change to event.preventDefault():
if (event.preventDefault) {event.preventDefault():}
else {event.returnValue=false;}
I've had this happen a million times with IE caching the content, which doesn't help because there will be no errors or problems reported, it just won't work. Try something like this and see what happens:
$('#divplayer').load("/player/index.php?timestamp=" + (+new Date));

Attaching mouse events at the window object does not work in Internet Explorer 8

Oh hai, anyone knows why Internet Explorer (8) is not firing events attached to the window?
attachEvent("onmousedown", function(){alert("here")}); //Doesn't work.
onmouseup = function(){alert("here 2")}; //Guess what? Doesn't work too.
Of course, I don't even need to say that this works on Firefox, Opera and Chrome
addEventListener("mousedown", function(){alert("here")}, false)
onmouseup = function(){alert("here 2")};
attachEvent works when on a div or *insert here any DOM element*, but I need the event to be global.
Try attaching it to the document
It appears that IE8 (and below presumably) does not bubble mouse events to the window object. IE9 seem to have corrected this misbehavior.
Case 1: Binding the mouse event at the window object:
onmousedown = function() { alert(1); };
Works in all current browsers but not in IE8.
Live demo: http://jsfiddle.net/simevidas/nbtYy/
Case 2: Binding the mouse event at the document object:
Works in all current browsers and in IE8.
document.onmousedown = function() { alert(1); };
Live demo: http://jsfiddle.net/simevidas/nbtYy/2/
Try attaching it to the window object like
$(window).mouseup(callback)

trigger onresize in cross browser compatible manner

I would like to trigger the onresize event from my C# code behind. I think this can be done with
Page.clientScript.RegisterScriptBlock(this.getType(), "id", "javascript code");
I have tried element.onresize() but it doesnt seem to work in firefox. What is the correct way to trigger an onresize event similar to the following jQuery?
$("body").trigger("resize");
Using jQuery itself isn't an option.
This should do the trick, DOM Level 2, no idea whether this works in IE6, quirks mode still has no information on this stuff:
if (document.createEvent) {
var e = document.createEvent('HTMLEvents');
e.initEvent('resize', true, false);
document.body.dispatchEvent(e);
} else if (document.createEventObject) {
document.body.fireEvent('onresize');
}
Tested in FF, Chrome and Opera.
use this $(window).resize();
(tested in FF, chrome, IE8)
// old answer, fails in FF
document.body.onresize()

keydown EventListener in IE7

I've written this code inside the HEAD tags of my HTML page. It works fine in Firefox, Chrome and Safari, but doesn't in IE7. I would like to know how to fix it.
<script type="text/javascript">
if ( window.addEventListener ) {
window.addEventListener("keydown", function(e) {
alert(e.keyCode);
}, true);
}
</script>
Microsoft has implemented their own way of doing this called attachEvent. You can read more about this over at quirksmode.org: http://www.quirksmode.org/js/events_advanced.html
You're screwed: you're using event capturing (passing true as the last parameter to addEventListener). IE has no such equivalent, in any version, including IE8 in IE8 mode.
Is there a reason you must use event capturing rather that event bubbling here? IOW, pass false as your last parameter? Then, you'd be able to port this (somewhat) to use IE's attachEvent proprietary method, or use a library (as others have suggested and added links for).
There is no window.addEventListener in IE, you need to use attachEvent. There's good documentation on events here, or you could switch to using a library that abstracts away browser differences.
Try:
window.attachEvent
More fully:
//set page event handlers
if (window.attachEvent) {
//IE and Opera
window.attachEvent("keydown", "");
} else if (window.addEventListener) {
// IE 6
window.addEventListener("keydown", "");
} else {
//FireFox
document.addEventListener("keydown", "");
}

Categories