Question

I'm trying to add width and height to images that I have in old WYSIWYG FCKEditor. The problem is, when I'm trying to get naturalWidth/Height that way I got 0 values.

What am I doing wrong?

here is the code:

var zaj = FCKeditorAPI.GetInstance('hometext');

pz =  zaj.GetXHTML();

dom_zaj = document.createElement('div');
dom_zaj.innerHTML = pz;

$(dom_zaj).find('img').each(function(i, element) {
    //var w_set = $(element).attr('width');
    //var h_set = $(element).attr('height');
    var w_native = element.naturalWidth;
    var h_native = element.naturalHeight;

    $(element).attr('width', w_native);
    $(element).attr('height', h_native);
});
Was it helpful?

Solution

I manage to fix the problem on my own. Since the pic's where in editor, I had to load them manualy by myself using jquery.onload(); (and changed from width/height to css styles)

var $images = $(dom_zaj).find('img');
    var imagesLength = $images.length;
            $images.load(function(){
        var w_native = this.naturalWidth;
        $(this).css('width', w_native+'px');
        imagesLength = imagesLength-1;
        if(imagesLength === 0){
        pz = dom_zaj.innerHTML;
        zaj.SetHTML(pz);
        }

            }).error(function(){
        imagesLength = imagesLength-1;
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top