insertAdjacentHTML in Safari

R

Rauan Maemirov

For ff and opera I'm extending HTMLElement's prototype by this:

if(typeof HTMLElement!="undefined" && !
HTMLElement.prototype.insertAdjacentElement) {

HTMLElement.prototype.insertAdjacentElement = function
(where,parsedNode) {
switch (where) {
case 'beforeBegin':
this.parentNode.insertBefore(parsedNode,this)
break;
case 'afterBegin':
this.insertBefore(parsedNode,this.firstChild);
break;
case 'beforeEnd':
this.appendChild(parsedNode);
break;
case 'afterEnd':
if (this.nextSibling) {
this.parentNode.insertBefore(parsedNode,this.nextSibling);
} else {
this.parentNode.appendChild(parsedNode);
break;
}
}
}

HTMLElement.prototype.insertAdjacentHTML = function (where,htmlStr) {
var r = this.ownerDocument.createRange();
r.setStartBefore(this);
var parsedHTML = r.createContextualFragment(htmlStr);
this.insertAdjacentElement(where,parsedHTML)
}

HTMLElement.prototype.insertAdjacentText = function (where,txtStr) {
var parsedText = document.createTextNode(txtStr);
this.insertAdjacentElement(where,parsedText);
}
}


But it doesn't work on Safari (v3.04 for Windows). What I'm doing
wrong?
 
R

RobG

For ff and opera I'm extending HTMLElement's prototype by this:

if(typeof HTMLElement!="undefined" && !
HTMLElement.prototype.insertAdjacentElement) {

[...Safari skips this block...]
        }

        HTMLElement.prototype.insertAdjacentHTML = function (where,htmlStr) {
                var r = this.ownerDocument.createRange();
                r.setStartBefore(this);
                var parsedHTML = r.createContextualFragment(htmlStr);
                this.insertAdjacentElement(where,parsedHTML)
        }

        HTMLElement.prototype.insertAdjacentText = function (where,txtStr) {
                var parsedText = document.createTextNode(txtStr);
                this.insertAdjacentElement(where,parsedText);
        }

}

But it doesn't work on Safari (v3.04 for Windows). What I'm doing
wrong?

Adding the following after your code and running it in Safari may give
you a hint:

var e = HTMLElement.prototype;
alert(
typeof e.insertAdjacentElement // function
+ '\n' + typeof e.insertAdjacentHTML // undefined
+ '\n' + typeof e.insertAdjacentText // undefined
)

Safari does allow element constructor prototypes to be modified, you
just have to do it differently to how Gecko and Opera do it. The link
below offers a solution, you should be able to get rid of the browser
sniffing and turn it into something more useful. The second link may
help too.

<URL: http://www.codingforums.com/archive/index.php?t-60406.html >

<URL: http://my.opera.com/_Grey_/blog/2007/04/21/safari-and-htmlelement-prototype
 
R

Rauan Maemirov

I've seen given links and couldn't understand how to assign
methods(i.e. functions like insertAdjacentText). There's only
constructors. Could u give me more samples?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top