Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check if browser supports createContextualFragment

Status
Not open for further replies.

philcha

Programmer
May 10, 2000
109
GB
The W3C Dom deprecates the innerHTML property supported by IE. A few articles recommend using for DOM-compliant browsers a subroutine which uses createContextualFragment. But createContextualFragment is not part of the W3C DOM, it is a Netscape specific add-on which is supported by all Gecko-based browsers. Hence a pure W3C DOM browser would not support it.

How can I check (without needing a catalogue of all browsers ever made plus full specifications and bug lists) whether a browser supports createContextualFragment? I'm looking for something as easy as the well-known
if (document.getElementsByTagName("*")) w3cdom = true
 
I realize this doesn't answer your exact quetion, but I hope you don't mind a reply. I can offer you a suggestion to post this question at this forum as well:

I found this turorial:
They did it by detecting NS6
//if IE 4+
if (document.all)
//else if NS 4
else if (document.layers){
//else if NS 6 (supports new DOM)
else if (document.getElementById){
tmlFrag = rng.createContextualFragment(mycontent);

see this page:


It sure looks like a lot more trouble than it's worth. I can't beleive that W3C would opt for that method instead of depreciating something like innerHTML.

As you know, Mozilla and IE still support innerhtml.

BTW, do you have a link to the exact page of the current DOM Specifications. I've always had a hard time figuring out what page to read at W3C on the specifications. I'm interested in finding out why innerHTML is deprectiated and what the replacement is (which they don't usually do unless it's better like CSS).
 
Could you provide a working example of something using createcontextualfragment (from <html> tag to </html> tag)?

Also, if you have a page that explains createcontextualfragment that would also be of help.
 
KevinAr18,

I'll try webxpertz.net

I searched the web (&quot;w3c dom innerhtml&quot;) and found only 1 page which threw some light on why W3C didn't adopt something like innerHTML:
* innerHTML is HTML specific, while the DOM is meant to support any language (e.g. XML) and any environment( not just browsers).
* innerHTML bypasses the DOM - 1 statement using innerHTML can do the work of a recursive subroutine which walks the node tree.
They both sound pretty feeble reasons to me.

There's a good DOM reference at A lot of DOM references and tutorials don't mention createContextualFragment because it's a nonstandard Netscape extension. But on the bright side that means it's easy to look up createContextualFragment on the web and find examples and explanations.
 
Hi, everybody

Got an answer at -
if (typeof window.Range != 'undefined' && typeof Range.prototype.createContextualFragment == 'function') {
// your code here
}

This gives the right results in Netscape 6 (DOM with createContextualFragment) and IE 5.0 (non-DOM).

Does anyone have a DOM-compliant browser which does not support createContextualFragment in which they could test the script? All the DOM-compliant Windows broswers appear to based on Gecko, which supports createContextualFragment.
 
I'm afraid that Gecko-based browsers are the only one that support createContextualFragment and therefore I'd not recommend to base any important features on it. If we talk about cross-browser solutions, of course.
As for me, I avoid using all DOM features that are currently Gecko-only.
 
Starway,

In principle I'd like to agree with you. But what % of the market are DOM-compliant browsers which do not support createContextualFragment? Can you point me at some browser names (with platforms if not Windows) and some stats? I'm sure you've noticed that when even the so-called gurus say &quot;cross-browser&quot; they mean &quot;IE plus Netscape 4 plus Netscape 6 and compatibles&quot; - that's certainly what their sample code indicates. I suspect most developers follow their example and are satisfied if their pages don't create JS errors in minority browsers.

I think the W3C make a big mistake by not including in the DOM some method which gives access to the browser's parser. For example if you stick strictly to the DOM, you have to re-write the script if you want to insert different HTML. Or suppose you want to swap the HTML content of 2 blocks dynamically - you probably have to write an object with methods (recursive) to save the old content in some kind of hierarchical data structure and to insert it in the new location.

I expect most developers will use innerHTML and / or createContextualFragment, and the W3C will eventually have to approve a method of accessing the browser's parser (or user agent's parser, if the ua is not a browser). If the W3C don't move quickly they'll find everyone refusing to use anything but innerHTML, because Gecko-based browsers support both approaches and using innerHTML is a whole lot easier than using createContextualFragment.
 
If I'm right and createContextualFragment is supported only by Gecko-based browsers, then it's % will be very very low. Gecko-based browsers had just began their way.

W3C/DOM games is not a thing we can affect on so much. What we do is just following the general trends after all.

Regarding &quot;so-called gurus&quot;: if you are interested, search for my posts regarding this to get an idea what I mean by telling &quot;cross-browser&quot;.
 
philcha, if you read my reply at siteexperts (first time I've ever been there too, I usually hang out at CodingForums...), you'll notice that I mentioned the DOM3 Load and Save module.

This module defines several interfaces which allow directly working with the browser's parser and serializer. As well as dynamically loading XML files, etc.
 
Jason,

I read your reply re DOM 3 Load & Save Module, and have done a little research on the web.

The most recent reference I could find which had a date was Feb 2002, and that referred to DOM 3 Load & Save Module as a work in progress. Is it an offical standard yet? If not, when is it expected?

I also checked up on browser support for XML and the most informative article I found (unfortunately date Jan 2000) is at This gave the impression that IE 5 and Netscape 6 provide patchy XML support and the other browsers are nowhere.

So my impression is that the DOM 3 Load & Save Module might be worth considering in a couple of years or so, when the standard is defined and most users have upgraded to browser which support it well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top