Panasonic Youth rob sanheim writes about software, business, ruby, music, stuff and things



Posted
23 May 2006 @ 1am

Tagged
Ajax, Javascript

Discuss

Browser bugs - onblur and onfocus with IFrames

This is one of those cross browser issues that you think you should be able to find via Google within five minutes, but ends up taking much longer. Maybe I was using poor search queries, or maybe I'm just having an off day. Anyways, maybe this will helpful for someone in the future...

I'm using a rich editor widget, TinyMCE, and have been trying to attach validation to the editor onblur. The documentation at MCE's website was spotty at best, with most suggestions saying "write a plugin" for something that should not be that complicated. So I decided to try and attach the handler directly to the iframe that tinymce is using under the covers for its magic. So here is the first, simple attempt:

JAVASCRIPT:
  1. var theFrame = $('mce_editor_0');
  2. Event.observe(theFrame.contentDocument, 'blur', testIt);

...Which only worked in IE6, not in FF (mac or win). This is the point where I should have stopped and just gone to the IFrame part at the Gecko Dom page, as the lack of any events on the element would've tipped me off. But I didn't. I went to google and tried a bunch of queries: "iframe onblur", "iframe onblur event", "attach events iframe", etc. This got me a lot of stuff about different ways to access iframes to make old browsers happy, and a bunch of stuff about design mode and rich text editing, but nothing about where the heck to attack handlers for onblur or onfocus (or anything else) in Gecko vs IE.

Then I just went to Firebug and did some inspecting on the actual iframe, and through trial and error found the ridiculously simple solution - attach events to contentdocument. In code:

JAVASCRIPT:
  1. var theFrame = $('mce_editor_0');
  2. if(theFrame.contentDocument) {
  3.     Event.observe(theFrame.contentDocument, 'blur', testIt);
  4. } else {
  5.     Event.observe(theFrame, 'blur', testIt);
  6. }

Totally simple, and if you have hit this snag its probably second nature. The first time, though, its pretty painful. And of course, this still

3 Comments

Posted by
James
8 February 2007 @ 4pm

Livesaver! I was searching for a way to bind a blur event and came across this. However I am dynamically creating my TinyMCE instances so I modified your code a bit to accomodate dynamic editor instances.

function edit(obj){

tinyMCE.idCounter=0; //reset the internal id counter to handle multiple instances of the editor
tinyMCE.execCommand(’mceAddControl’,true,obj.id);

var theFrame = tinyMCE.getInstanceById(obj.id);

if(theFrame.contentDocument) {
Event.observe(theFrame.contentDocument, ‘blur’, testIt);
} else {
Event.observe(theFrame, ‘blur’, testIt);
}
}


Posted by
steve
11 March 2007 @ 12pm

Hi,

thanks for this tip, this is exactly the problem I have. I haven’t been able to get your solution to work. I get an error “\xD4blur\xD5 is not defined” have you run into this?

thanks!

In the other ways of assigning it:
window.frames[wysywig_iframe_name].onblur = update_wysiwyg; //FF
document.getElementById(wysywig_iframe_name).onblur = update_wysiwyg; //IE

I seem to be assigning the event handler and can test that it is assigned, but no luck with it firing!

I have created a test page and it works fine, but not in the RTE version -perhaps this is capturing the event somehow.

You are totally right that this is confounding! any help would be appreciated

thanks,
steve


Posted by
JelleR
28 May 2007 @ 3pm

I have the problem that i can cath the onblur event of the TinyMCE, but this event is also generated if you click f.i. on the select to get a headertype, background-color or textcolor. So clicking the buttons of tinyMCE itself also generates onblur events.
I do a
execCommand(’mceRemoveControl’)
upon a onblur event. So now my tinyMCE disappears also when i choose another color/font/etc.
Anyone has a solution?


Leave a Comment

An Open (Love) Letter to Taco Bell’s Crunch Wrap Supreme Using Mac OS X Color Picker as a stand alone app