Jump to content

Adding to DOM objects


shadowayex

Recommended Posts

I'm working on a library that does some manipulations on DOM objects. The library does these manipulations down the DOM line, so I am aiming to write it object-oriented like. Based on my knowledge, I started designing it as wrapper objects that had a field devoted to the item itself, but I was wondering if it would be possible and better to just add my functions and fields to the DOM objects themselves.I don't really know too much about DOM or conventions concerning DOM objects or OOP in JavaScript, so the more information, the better.

Link to comment
Share on other sites

You may, and it would be better... if you're OK with your library not working in IE7 and below.If you want it to work in IE7 and below, you'll have to have separate functions... like everybody else... if you've ever wondered why even simple libraries are written as separate functions, now you know.

Link to comment
Share on other sites

You may, and it would be better... if you're OK with your library not working in IE7 and below.If you want it to work in IE7 and below, you'll have to have separate functions... like everybody else... if you've ever wondered why even simple libraries are written as separate functions, now you know.
Ah, IE7 >_<Well, I'll probably write them as wrapper objects for now, and then when IE7 becomes less popular, I can convert it. But it's nice to know the capability is there. Thank you.
Link to comment
Share on other sites

IE7 is very less popular... it's IE6 that's the real problem... people that have successfully updated to IE7 have had a smooth migration path to IE8, partly due to IE8's compatibility view, but that's not the case for IE6. So the real question is if you can abandon IE6.

Link to comment
Share on other sites

IE7 is very less popular... it's IE6 that's the real problem... people that have successfully updated to IE7 have had a smooth migration path, partly due to IE8's compatibility view, but that's not the case for IE6. So the real question is if you can abandon IE6.
Well, I was never a huge fan of IE6, and most of the people that will be seeing my library within the next year will probably not be using IE6, as they are either tech savvy enough to know better, or they take advice from me. So, that being said, if after this next year IE6 won't be that popular, then I can probably safely abandon IE6.I don't know nearly as much about browser statistics than I should. But, based on my knowledge of JavaScript and the nature of the project, I can say that it probably will not be openly useful for the next few months, and I probably won't actively promote it for a few months after that, as I expect there will be uncaught issues that will need worked out.How long do you think support for IE6/7 will be an issue?
Link to comment
Share on other sites

Hard to say... I sometimes have clients that use Windows 98 because of legacy DOS applications, and IE6 is the best IE version you can have on that. Other browsers have removed support for Windows 98, making the situation even worse.I keep advising all of those people to upgrade at least to XP, but it's not up to them in many cases... it's up to the (missing!) programmers of their legacy apps. There's often not an alternative app on the market, or if there is, it costs what those people would consider "a fortune", and it's hard to justify spending a fortune for something which you already have working.Windows 98 excluded, it depends on how quickly people migrate to Windows 7, which would be anywhere between now and... I think 2014 (whenever XP support ends).

Link to comment
Share on other sites

Hard to say... I sometimes have clients that use Windows 98 because of legacy DOS applications, and IE6 is the best IE version you can have on that. Other browsers have removed support for Windows 98, making the situation even worse.I keep advising all of those people to upgrade at least to XP, but it's not up to them in many cases... it's up to the (missing!) programmers of their legacy apps. There's often not an alternative app on the marker, or if there is, it costs what those people would consider "a fortune", and it's hard to justify spending a fortune for something which you already have working.
Good point. I ran across a couple Windows 98 users in the last 6 months. I suppose the wrapper option would be best for compatibility, but I can try to write it to be easily ported.I'm sure this won't be a problem, but if/when I can switch to adding it to the DOM object, is there anything guarding against me overwriting something already existent? Is there a convention I should be thinking about? I thought about adding them in a manner such as object.myLibraryName.myFunction(), would that be a good route?After I get a stable wrapper version, I'll probably spend some time on the DOM addition version, so I'll have something for when I can safely switch.
Link to comment
Share on other sites

There is sort of a guard... you can test for all of your objects, and not override them if they exist... like:

if (!document.prototype.getElementByTagName) {	//document.getElementByTagName is not available. Implement it.	document.prototype.getElementByTagName = function() {		//implementation	}}else {	//document.getElementByTagName is available... if that's a problem, workaround here.}

You can't really know if the function does what you expect it to do though... the only way to check that is to actually execute the function with some sample data and compare the actual output with the expected one.

Link to comment
Share on other sites

There is sort of a guard... you can test for all of your objects, and not override them if they exist... like:
if (!document.prototype.getElementByTagName) {	//document.getElementByTagName is not available. Implement it.	document.prototype.getElementByTagName = function() {		//implementation	}}else {	//document.getElementByTagName is available... if that's a problem, workaround here.}

You can't really know if the function does what you expect it to do though... the only way to check that is to actually execute the function with some sample data and compare the actual output with the expected one.

Ah, alright, that makes sense. When adding things to objects, should it be done to the prototype, or to the object itself? For instance, should it be object.myFunction = function() { //whatever }; or object.prototype.myFunction = function() { //whatever }; ?
Link to comment
Share on other sites

If you add it to the prototype, then any object of that type that you create will have it. If you add it to an individual object then only that one will have it. For the document object it doesn't really matter, because much like Highlander there can be only one, but you could extend the array or string prototype and then any array or string you create would have the additional methods and properties.

Link to comment
Share on other sites

If you add it to the prototype, then any object of that type that you create will have it. If you add it to an individual object then only that one will have it. For the document object it doesn't really matter, because much like Highlander there can be only one, but you could extend the array or string prototype and then any array or string you create would have the additional methods and properties.
Ah, alright. Thanks for all the help guys =)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...