Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #15695 > unrolled thread
| Started by | Bwig Zomberi <zomberi@email.invalid> |
|---|---|
| First post | 2012-09-05 10:07 +0530 |
| Last post | 2012-09-06 09:36 +0530 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.javascript
How to correctly add IE support for standard but missing function Bwig Zomberi <zomberi@email.invalid> - 2012-09-05 10:07 +0530
Re: How to correctly add IE support for standard but missing function David Mark <dmark.cinsoft@gmail.com> - 2012-09-05 04:37 -0700
Re: How to correctly add IE support for standard but missing function Bwig Zomberi <zomberi@email.invalid> - 2012-09-06 09:36 +0530
| From | Bwig Zomberi <zomberi@email.invalid> |
|---|---|
| Date | 2012-09-05 10:07 +0530 |
| Subject | How to correctly add IE support for standard but missing function |
| Message-ID | <k26kv4$dbk$1@speranza.aioe.org> |
Hi Group,
Some versions of IE do not support certain DOM methods and properties
available in all other browsers. I wish to add support for these API in
a single function call so that the rest of my JS code is free of JS
hacks.
Here is my code. There must be a simpler way than adding the missing
methods and properties to every element in the page.
function fixIE() {
var i, n;
var oAllElements = document.getElementsByTagName("*");
n = oAllElements.length;
if (!document.getElementsByClassName) {
for (i = 0; i < n; i++) {
oAllElements[i].getElementsByClassName =
myGetElementsByClassName;
}
document.getElementsByClassName =
myGetElementsByClassName;
}
if (!document.getElementsByTagName("body")[0].textContent) {
for (i = 0; i < n; i++) {
oAllElements[i].textContent =
oAllElements[i].innerText; // innerText works for me
}
}
}
function myGetElementsByClassName(sClassName) {
var oAllElements = this.getElementsByTagName('*');
var aMatchingElements = [];
var i, n;
n = oAllElements.length;
for(i = 0; i < n; i++){
if (
(oAllElements[i].className == sClassName) ||
((' ' + oAllElements[i].className + ' ').indexOf(sClassName)
> -1) ||
((' ' + oAllElements[i].className).indexOf(sClassName,
oAllElements[i].className.length - sClassName.length) > -1)
) {
aMatchingElements.push(oAllElements[i]);
}
}
return aMatchingElements;
}
[toc] | [next] | [standalone]
| From | David Mark <dmark.cinsoft@gmail.com> |
|---|---|
| Date | 2012-09-05 04:37 -0700 |
| Message-ID | <d562b0ea-d9f4-4057-bc27-c7f4dd40b9c2@v15g2000yqi.googlegroups.com> |
| In reply to | #15695 |
On Sep 5, 12:37 am, Bwig Zomberi <zomb...@email.invalid> wrote:
> Hi Group,
>
> Some versions of IE do not support certain DOM methods and properties
> available in all other browsers. I wish to add support for these API in
> a single function call so that the rest of my JS code is free of JS
> hacks.
>
> Here is my code. There must be a simpler way than adding the missing
> methods and properties to every element in the page.
>
> function fixIE() {
> var i, n;
>
> var oAllElements = document.getElementsByTagName("*");
> n = oAllElements.length;
>
> if (!document.getElementsByClassName) {
> for (i = 0; i < n; i++) {
> oAllElements[i].getElementsByClassName =
> myGetElementsByClassName;
> }
> document.getElementsByClassName =
> myGetElementsByClassName;
> }
>
> if (!document.getElementsByTagName("body")[0].textContent) {
> for (i = 0; i < n; i++) {
> oAllElements[i].textContent =
> oAllElements[i].innerText; // innerText works for me
> }
> }
>
> }
>
> function myGetElementsByClassName(sClassName) {
>
> var oAllElements = this.getElementsByTagName('*');
> var aMatchingElements = [];
> var i, n;
> n = oAllElements.length;
>
> for(i = 0; i < n; i++){
> if (
> (oAllElements[i].className == sClassName) ||
> ((' ' + oAllElements[i].className + ' ').indexOf(sClassName)
> > -1) ||
> ((' ' + oAllElements[i].className).indexOf(sClassName,
> oAllElements[i].className.length - sClassName.length) > -1)
> ) {
> aMatchingElements.push(oAllElements[i]);
> }
>
> }
>
> return aMatchingElements;
>
>
>
>
>
>
>
> }
Use wrappers. Do *not* augment host objects. You don't own them and
you certainly don't want to try to implement 100% of the standard
functionality (just implement what you need). Besides host objects are
allowed to throw exceptions just for *reading* their properties (and
some do just that in IE).
When you hear: you can just use a "polyfill", that's a recommendation
do something silly (like the above).
Examples of cross-browser DOM wrappers:
https://github.com/rassie/jessie
[toc] | [prev] | [next] | [standalone]
| From | Bwig Zomberi <zomberi@email.invalid> |
|---|---|
| Date | 2012-09-06 09:36 +0530 |
| Message-ID | <k297ft$i2r$1@speranza.aioe.org> |
| In reply to | #15706 |
David Mark wrote: > Use wrappers. Do*not* augment host objects. You don't own them and > you certainly don't want to try to implement 100% of the standard > functionality (just implement what you need). Besides host objects are > allowed to throw exceptions just for*reading* their properties (and > some do just that in IE). Thank you, David. I also realize that does not work for dynamically created page elements. -- Bwig Zomberi
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web