Get default display value of an element

[2011-04-28 21:04:26 --@Javascript]

Recently, In a project,operation fails in the iframe,the reason is that sub-domain is inconsistent, such a.xxx.com with "xxx.com", solution is to set document.domain to "xxx.com".

Yesterday, I noticed the effect part of the defaultDisplay function jQuery code has been rewritten, more rigorous and reasonable. Using the iframe related 
technologies, that is, to find a clean environment free of interference patterns obtained by other elements of the default display value,the code is similar to the following:
useIframeGetdefaultDisplay = function() {
        var iframe, iframeDoc, display;
        return function(nodeName) {
                if (!iframe) {
                        iframe = document.createElement("iframe");
                        iframe.frameBorder = iframe.width = iframe.height = 0
                }
                document.body.appendChild(iframe);
                if (!iframeDoc || !iframe.createElement) {
                        iframeDoc = iframe.contentWindow.document;
                        iframeDoc.write("")
                }
                iframeDoc.close();
                elem = iframeDoc.createElement(nodeName);
                iframeDoc.body.appendChild(elem);
                display = elem.currentStyle.display;
                document.body.removeChild(iframe);
                return display;
        }
} ();


The useIframeGetdefaultDisplay function work cross-browser well.
But in IE: If the page sets domain, The page throws an Uncaught Error: SECURITY_ERR, see 

http://bugs.jquery.com/ticket/8985
.

So,
how to get default display value of an element regardless of domain set?

Disable the styleSheets?
A corresponding list of default display?

But Any other clean environment in addition iframe?

createPopup? createPopup does not support appendChild ...
showModalDialog? no!

...

DocumentFragment?
Try and found it works in IE! DocumentFragment is a pure space we want(NOTE:it's not pure space in !IE): 

/**
 * Get default display value of an element.
 * @author cmc3cn (http://cmc3.cn)
 * @updated 28-Apr-11
 */
useFragmentGetdefaultDisplay = function() {
        var fragment = document.createDocumentFragment(),
        tmp,
        display;
        return function(nodeName) {
                tmp = document.createElement(nodeName);
                fragment.appendChild(tmp);
                display = tmp.currentStyle.display;
                fragment.removeChild(tmp);
                return display;
        };
} ();

useFragmentGetdefaultDisplay('span') // inline
useFragmentGetdefaultDisplay('li') // IE8:list-item ,IE<8: block


COMPARE THE VALUE return by useIframeGetdefaultDisplay and useFragmentGetdefaultDisplay:
node default display value

本文禁止复制/转载!

共 4 评论

1. 2011-04-29 20:04:33,from Helina

英语不错啊 ,我们四级没过的



2. 2011-05-05 14:05:37,from snandy

菜菜太牛了。以你的英文和技术水平可以进雅虎了。



3. 2011-05-21 02:05:59,from Charl

Never seen a beettr post! ICOCBW



4. 2011-10-28 17:10:03,from bobbyjohnsonelch

Hi everyone. This is my very first visit to this excellent place.
Thanks for the neat info.
[url=http://erotichypnosis.org/forums/User-uSashaPittmanx]brett favre[/url]



新增评论

分类导航