Forum Moderators: open

Message Too Old, No Replies

Element.prototype in IE

         

Jesse

1:11 pm on Nov 17, 2008 (gmt 0)

10+ Year Member



Hi,

I've defined some functions for HTML elements in my script using:

Element.prototype.somefunction= function(){function_body}
etc.

It works brilliant in firefox but of course internet explorer has issues with it. I get the error: 'Element is not defined'.

I read on some blog that this can be fixed with adding a 'HTC file' but I didn't really understand the explanation of how to do it.

Is there someone who can explain this to me or, better, who has an easier solution for this problem?

tnx

astupidname

5:47 pm on Nov 17, 2008 (gmt 0)

10+ Year Member



You have not provided enough info about your code to tell you explicitly where you are going wrong, so please in the future.. post some more code so people can help tell you more accurately where you are going wrong. I don't know why it does not work for you, but it does work. Do you have a constructor function defined in your script with the name Element? Try the following out, and welcome to webmasterworld!

<script type="text/javascript">
window.onload = function () {stinky.weirdnoise(); stinky.strangenoise();};

function Element(){
this.betterGetThatChecked = "prhraaappp bbloooop splaupp";
var badsound = "phhoooootttt";
this.weirdnoise = function () {
alert(badsound);
};
}

Element.prototype.strangenoise = function () {
alert(this.betterGetThatChecked);
};

stinky = new Element();

</script>

daveVk

11:22 pm on Nov 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Prototyping on DOM elements does not work in IE.

I think you can manually add functions to each element you need it on

function fred( ) { ... }
...
element.somefunction = fred; // after createElement or loop thru existing elements

HTC is a non standard oddity I would avoid.

Jesse

10:36 am on Nov 19, 2008 (gmt 0)

10+ Year Member



Thanks for your response. I'll post some more code. My function is:

Element.prototype.getParentBy=function(prop, val){

a=this.parentNode
while(a.tagName!="BODY"){
if(a[prop]==val){ return a;}
a=a.parentNode;
}
return false;
}

it searches for the first parent of an element with the specified property and value, i.e.
someHTMLelement.getParentBy("className", "container")

I really don't want to define this function for every specific element I might want to use it on so that's no solution.

I hope this is enough information. It works fine in firefox and I'd really like to get it to work in IE also, so any solution, even one with a 'HTC' file (allthough I have no idea whatthose are) would be greatly appreciated.

daveVk

4:10 am on Nov 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would cop out and rewrite it as a function

function getParent( Element, prop, val){ ... }

Not great but better than this ? [msdn.microsoft.com...]

Jesse

7:48 am on Nov 20, 2008 (gmt 0)

10+ Year Member



Yeah, I thought about rewriting it but this is not the only such function I have in my script. They make my code very neat and tidy the way they are. I think I'll look into the HTC. It certainly looks like a 'nonstandard oddity', as you said, but hey, so is internet explorer. Thanks for the resource link.

daveVk

2:47 am on Nov 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It may be worth looking inside one of framework libraries that provide getElementByClassName for tricks.