473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

HTMLElement.prototype in Internet Explorer

Is there a way I can add a setter/getter to the HTMLElement prototype
in internet explorer?

This, for example, works fine in Mozilla:

HTMLElement.prototype.__defineSetter__("foobar", function (sFoobar)
{
alert( sFoobar );
});
Of course my method I want to add is more complex, the question is is
there any way at all to do this in IE6?
Jul 23 '05 #1
2 8503


Jason Keirstead wrote:
Is there a way I can add a setter/getter to the HTMLElement prototype
in internet explorer?

This, for example, works fine in Mozilla:

HTMLElement.prototype.__defineSetter__("foobar", function (sFoobar)
{
alert( sFoobar );
});
Of course my method I want to add is more complex, the question is is
there any way at all to do this in IE6?


There is no
HTMLElement
exposed in IE's object model, there is no prototype for host objects
exposed in IE's object model, and the JScript engine doesn't support
__defineSetter__ as that is a JavaScript 1.5 extension to the ECMAScript
standard.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Martin Honnen wrote:
Jason Keirstead wrote:
Is there a way I can add a setter/getter to the HTMLElement prototype
in internet explorer?

This, for example, works fine in Mozilla:

HTMLElement.prototype.__defineSetter__("foobar", function (sFoobar)
{
alert( sFoobar );
});
Of course my method I want to add is more complex, the question is is
there any way at all to do this in IE6?


There is no
HTMLElement
exposed in IE's object model, there is no prototype for host objects
exposed in IE's object model, and the JScript engine doesn't support
__defineSetter__ as that is a JavaScript 1.5 extension to the ECMAScript
standard.


Although the following is possible:

<script type="text/javascript">
var type, elements;
if (((type = typeof document.getElementsByTagName) == 'function' ||
(type == 'object' && document.getElementsByTagName)) &&
!!(elements = document.getElementsByTagName('*'))) {
var i = elements.length;
while (i--) {
elements[i].setFoobar = function(sFoobar) {
alert(this.nodeName + ';' + sFoobar);
}
}
}
document.body.setFoobar('this is a test');
</script>

Yes, I understand it is not the same as defining a setter for the
HTMLElement.prototype, but it does allow you to attach a method to every
element on the page, and it works in IE6SP1, Gecko-based browsers (Firefox,
Mozilla, Camino, Netscape 6+) and Opera 7.54. It even works in Opera 6.05,
although nodeName returns "undefined" so it is of limited usefulness there.

Of course any new elements added to the page programmatically will not have
the function as part of it's prototype, although you could add the function
when you construct the element:

var myDiv = document.createElement("div");
myDiv.setFoobar = function(sFoobar) {
alert(this.nodeName + ';' + sFoobar);
}

That condition would probably be clearer written as:

var type = typeof document.getElementsByTagName;
if (type == 'function' || (type == 'object' &&
document.getElementsByTagName) {
var elements = document.getElementsByTagName('*');
if (elements) {
// ...
}
}

Lastly. Of course my code can have a dramatic impact on performance on a
complex page. However, I have my doubts that you would need a setter on
*every* HTML element on the page, perhaps you would only want it on 3 or 4
nodeNames. In that case, it would probably be best to retrieve
document.getElementsByTagName('div'), 'td', 'a', etc for only the nodeNames
you actually need the setter attached to.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Neil Morris | last post by:
I would like to know the benefits for javascripts' prototype objects as compared to say java class-based objects. The main points that I have on my mind are: 1. Is there a way to mark properties...
1
by: Christopher Benson-Manica | last post by:
How do you extend HTMLElement? Why can't you just do HTMLElement.prototype.foo=function() { alert( 'foo' ); } I assume HTMLElement belongs to some kind of namespace, but darn if I can find...
3
by: VK | last post by:
Internet Explorer 7 beta 2 preview CNET Editor review: <http://reviews.cnet.com/Internet_Explorer_7_for_XP_SP2_Beta_2/4505-3514_7-31454661-2.html?tag=nl.e415> Summary (my personal review...
11
by: shypen42 | last post by:
Hi all, I'm very confused by the relation between "prototype" and that "Prototype.js" library that seems to be used quite a lot (not by knowledgeable people from this group if I understood...
0
by: JS | last post by:
I'm trying to figure out how to draw a rectangle around an HtmlElement in the WebBrowser control. Does anybody know how to do this? The behavior I'm trying to replicate is that of the Internet...
5
by: Gerry Vandermaesen | last post by:
Hi, Does anyone have a freely available JavaScript JSON stringifier. So far my search has been in vain, the one offered on http://www.json.org/json.js does not seem to work for me.
3
by: Robert | last post by:
Hi, I thought I pretty much understood the whole prototype chain stuff, but now I stumbled upon a difference between IE and Firefox, that is totally confusing me. An example.......
7
by: Yansky | last post by:
I'm just starting to learn about the prototype object in javascript and I was wondering if someone could explain just in laymans terms why you would use it instead of a regular functions? It seems...
5
by: marchaos | last post by:
I'm trying to override the native HTMLElement.addEventListener in firefox so that I can do some extra things. I know it's possible to add methods to the HTMLElement prototype and for them to be...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.