How to check if onresize method is set? - javascript

Can I check if a onresize method was set?
I've previously used:
$(window).resize(function() { /* ... */ });
Due a unknown bug in another library, onresize is not called anymore. After executing above line it works perfectly again. The method is invoked once. If I execute the line in the Firebug console again, the method is invoked twice.
I would like to write a workaround, which sets onresize as soon as it's "reseted".
I'm looking for something like that: (undefined or null)
if (window.onresize == undefined) { /* ... */ }
The external library/framework is Richfaces 4 (Extended Data Table). As soon as I sort a column, some of the onresize function handler were gone.
Before:
$._data(window,'events').resize
// result on the Chrome console:
[Object, Object, Object, Object]
After using sorting:
$._data(window,'events').resize
// result on the Chrome console:
[Object]
I'm looking for a way to write a workaround.
JIRA Issue
https://issues.jboss.org/browse/RF-13117 (fixed with future release 4.3.4)

You could use $._data() which is not a method publicly supported:
$(window).on('load',function() {
if(!$._data(window,'events').resize)
alert('none resize bound');
});
In older jquery version it was: $.data()

what i used in my project was
$(window).unbind('resize').bind('resize',function(){
//code here
});
it will remove all the previously bind (registered) handlers for resize event and register this new function as the handler.
this approach is useful only when you want to attach a single event handler.

Thanks for all answers and comments. As suggested I went to the source of the problem and wrote for that a workaround including opening an issue.
window.RichFaces.ui.ExtendedDataTable.prototype.deActivateResizeListener = function() {
if (this.resizeEventName != undefined) {
$(window).off(this.resizeEventName);
}
};
I accepted roasted answer since it really helped to find a workaround and his answer answered my question if there is a way to find out if a event handler is attached.

Related

Passing event object to debounced function in IE8

I want to debounce a keyup event on a table of rows which causes an AJAX call. I have used all debouncing plugins out there, including the one for jQuery by Ben Alman, the one from Underscore.js, the jQuery delayed() plugin, as well as one plugin from Filatov Dmitry which extends jQuery (like Ben Alman's).
My code looks like this:
function onKeyUp(evt) {
doSomethingWith(evt, true);
}
$('#mytable').on('keyup', $.debounce(500, onKeyUp));
The problem is that, while it works fine on Firefox and IE9, it doesn't work in IE8. Specifically, IE8 throws a "Member not found" error when I call evt.preventDefault() which goes into the jQuery 1.11.1 code and breaks in line 4967 on e.returnValue = false; (because preventDefault() apparently doesn't exist in IE8). Upon inspection of the event variable with the IE developer tools debugger, it seems that the variable contains all event member methods and properties, but most of them are marked as "Member not found".
I've alread tried this solution https://stackoverflow.com/a/3533725/134120 but it did not work.
Googling for "IE member not found" returns a lot of results, but not many solutions.
So, any ideas?
I need to first prevent the default handler (i.e. no scrolling) and then debounce my event handler.
Then use this:
var onKeyUp = $.debounce(500, doSomethingWith);
$('#mytable').on('keyup', function(evt) {
evt.preventDefault(); // do always
onKeyUp(evt, true); // possibly bounced call to doSomethingWith
});

IE attachEvent call returns true, but handler shows "null"

I have a fairly simple ASP.NET page that renders an HTML input (text box). Within the generated HTML, I attach a handler to several events, including onfocus, onkeypress, and onkeyup. Because this is a solution targeted at a version of IE that does not support addEventListener (situation about which I can do nothing), I am forced to use attachEvent.
A typical call to attachEvent is as follows - I've excerpted this source from the original for the sake of brevity/clarity, so it is not precisely the code at issue:
var hostControl = document.getElementById('mytextbox');
var attachResult = hostControl.attachEvent('onfocus', function(){
hostControl.select();
});
if (!attachResult)
{
alert('Attach failed.');
}
attachResult = hostControl.attachEvent('onblur', function(){
if (hostControl.value=='')
{
alert('Warning - no entry.');
}
});
if (!attachResult)
{
alert('Attach failed.');
}
When I step through this code in the IE debugger, attachEvent returns 'true' in both instances, which should indicate that the event attachment attempt was successful. However, when I look at the [Event] handlers for the control within the debugger, all the events show 'null', no handler attached.
Things I've tried/researched:
I've read several different articles on the vagaries of event attachment in IE, so I've speciously avoided any 'this' references.
I tried one version that used one of the addEvent wrapper blocks that tries to use addEventListener if available, even though I knew this would be an IE solution.
When I tried that version against FireFox, event attachment worked properly through addEventListener, but failed in IE using attachEvent (with attachEvent still returning true).
I then opted to eliminate any possible problems the wrapper might be introducing, and used attachEvent directly against the control, which leads me where I am now. The problem persists.
I would like to think I've simply overlooked something very simple, as I've hooked up events before without difficulty, but something here is throwing me a curveball I just don't recognize. Appreciate the extra eyeballs on this to see where I've erred.

Chrome Handling dataTransfer.types as Object

I'm having a really funky issue with dataTransfer in Chrome 24.0.1312.5 that I'm hoping I could get some ideas on. Basically, I have a nodeIterator that singles out some useful elements from an iframe. For each of these elements, I'm adding event listeners to permit dropping of content. I'm having a problem with this bit of code:
currentNode.addEventListener('dragover', function (e) {
// Only accept images
// Error occurs in below conditional
if (e.dataTransfer.types.contains('text/uri-list')) {
e.preventDefault();
}
});
Here's the portion of my code that adds the data:
figure.draggable = 'true';
figure.addEventListener('dragstart', function (e) {
e.dataTransfer.setData('text/uri-list', img.src);
e.dataTransfer.setData('text/plain', img.alt);
e.dataTransfer.setData('application/x-trash.delete', img.id);
});
The error I get in the console is this:
Uncaught TypeError: Object text/plain,text/uri-list,application/x-trash.delete
has no method 'contains'
I've written a fiddle that should be able to reproduce this issue, but of course it works just fine.
Any ideas of something I might be missing? Or other relevant portions of code I should check?
It seems that dataTransfer.types returned in Chrome is a Array.You can use indexOf() instead of contains method to workaround.

Get value of current event handler using jQuery

I can set the onclick handler using jQuery by calling
$('#id').click(function(){
console.log('click!');
});
Also using jQuery, how can I get a reference to the function which is currently handling the click() event?
The reason is that I have another object and want to set its click handler to the same one as #id.
Update
Thank you for all the suggestions. The problem is that I do not know which function is currently handling the clicks. Keeping track of it would add state to an already complicated template-editing system.
jQuery's .click(function) method adds the function to a queue that is executed on the click event~
So actually pulling out a reference to the given function would probably be hairy-er than you expect.
As noted by others, it would be better to pass in a reference to the function; and then you already have the reference you need.
var clicky = function () { /* do stuff */ };
$('#id').click(clicky);
// Do other stuff with clicky
Update
If you really really need to get it out, try this:
jQuery._data(document.getElementById('id')).events.click[0].handler
Depending on your version of jQuery that may or may not work~ Try playing around with
jQuery._data(document.getElementById('id'))
and see what you get.
Got the idea from this section of the source:
https://github.com/jquery/jquery/blob/master/src/event.js#LC36
if you dont know the name of the function you can use
args.callee
https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments/callee
function clickHandle(e){
if($(e.target) == $('#id')) {
$(newTarget).bind('click', clickHandle);
}
}
$('#id').bind('click',clickHandle);
I think this would be the most symantic way of going about it

js-hotkeys, function raised twice when combination is more than 1 key

I am using js-hotkeys. I have a problem where when my key combination is more than 1 key, eg. "Shift+Tab", my function is raised twice.
$("textarea").bind("keydown", "shift+tab", function() { ... });
See what happens here -> http://jsbin.com/osuza5/2/edit. seems like with 1 key it also triggers twice there.
This has happened since jQuery 1.4.2, it's a known issue. Luckily, John Resig forked this and created a much cleaner version a while back that also ...well - it works, you can check it out here.
Updating your jsbin to point at this plugin version: https://github.com/jeresig/jquery.hotkeys/raw/master/jquery.hotkeys.js (and that's the only change), it works. You can test it out here.
Try Code:
$("textarea").bind("keydown", "shift+tab", function(e)
{ e.preventDefault; bla..bla... });
Link: http://jsbin.com/osuza5/4/edit
Notice the combinations mentioned twice during binding. One along with the "keydown.shift+tab".
To keep it from firing multiple times in some browsers, unbind the hotkey in the end of the function. This worked for me.
I've used the original version from http://code.google.com/p/js-hotkeys/
Please note that using this will probably keep you from overriding any of the browser defaults. Hence, even though things will go as expected, but as soon as the unbinding occurs, the browser may switch tabs (shift+tab) because of this .
$("textarea").bind("keydown.shift+tab", "shift+tab", function() { ...
//Your Code Here
//this should be in the end
$("textarea").unbind("keydown.shift+tab", "shift+tab");
});

Categories