views:

287

answers:

2

Hey all,

I'm using jQuery to try and access the contents of an iframe WYSIWYG, just to get the character count and update a counter outside the iFrame. Oddly enough, my code works fine in in Firefox, but breaks in all versions of IE. Wondering if anyone could help me out with some IE-friendly syntax? Here's what I have so far:

This is in the onload function:

textCounterWYSIWYG('longDesc_cnt', 2000);

This is the function itself:

function textCounterWYSIWYG(text, limit) {
  var len = String($("iframe").contents().find("body").html());
  var trimmed = len.replace(/^\s+|\s+$/g, '');
  var length = trimmed.length;
  if (length > limit) {
    field.value = field.value.substring(0, maxlimit);
  }
  else {
    var rem = limit - length;
    $("#"+text).text(rem + " Characters Remaining");
  }
}

The var len = ... line is what seems to be breaking IE. Any thoughts/suggestions are highly welcome!

A: 

Perhaps it is some default IE security settings? What WYSIWYG library are you using?

prodigitalson
Currently using uEditor for jQuery. Not sure if it's security, the error IE gives of course seems to have nothing to do with where it actually is, so I'm not getting a detailed description of what's going on.
David Savage
Btw, the error Ie gives says object expected at the end of my jQuery include, doubt that helps any though, as I've seen it before when the actual error has nothing to do with jQuery.
David Savage
Are any of the plugins youre using "packed" as opposed to "minified" i know there are soem gotchas with packing that some plugin developers do not address.
prodigitalson
@prodigitalson: no, none of the plugins are packed. Many are minified, although it appears referencing the iFrame removes the error, although not actually fixing the character count script.
David Savage
A: 

First thing I would try is to get the iframe by id rather than by tag name.

Also, I'd change that variable name "len" to something more relevant ;-).

BobS
Adding an id to the iFrame fixed the error, and on the page load fixes the character count. However, on keyup it still calls the character count function, but no longer is updating the count, yet it works on the page load....
David Savage
It appears this is happening because whatever it's getting for the count isn't updating, even though using Firebug to inspect the element, the body is updating. I've even changed the javascript to reference the body ID: var len = String($("#theWYSIWYG").contents().find("#iframeBody").html());This always returns a 4 for the above "length" variable.
David Savage
Ah, further inspection reveals that by changing it to an ID, len's value is "null", which is why the count isn't being updated, but also not erroring out. So, apparently we're not really retrieving the contents of the iFrame using this method.
David Savage
Eureka! Apparently the problem was trying to call the text counter before IE had a chance to build the iFrame WYSIWYG. Case closed. :)
David Savage