insertAdjacentHTML in Safari

Discussion in 'Javascript' started by Rauan Maemirov, Mar 3, 2008.

  1. 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?
     
    Rauan Maemirov, Mar 3, 2008
    #1
    1. Advertising

  2. nobody uses Safari?
     
    Rauan Maemirov, Mar 6, 2008
    #2
    1. Advertising

  3. Rauan Maemirov

    RobG Guest

    On Mar 3, 11:01 pm, Rauan Maemirov <> wrote:
    > 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
    >



    --
    Rob
     
    RobG, Mar 6, 2008
    #3
  4. 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?
     
    Rauan Maemirov, Mar 7, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jane Hawkins

    Safari Display Problem

    Jane Hawkins, Dec 23, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    508
    Todd A
    Dec 23, 2003
  2. Hemlata Shah

    Safari Browser compatible with dotnet

    Hemlata Shah, Mar 3, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    625
    Hemlata Shah
    Mar 4, 2004
  3. Phil

    problem with insertAdjacentHTML

    Phil, Apr 27, 2004, in forum: Javascript
    Replies:
    3
    Views:
    156
    Mick White
    Apr 29, 2004
  4. dexter
    Replies:
    2
    Views:
    98
  5. Dmitry Kulinich

    how to insertAdjacentHTML/Element for iframe ?

    Dmitry Kulinich, Jun 6, 2007, in forum: Javascript
    Replies:
    1
    Views:
    215
Loading...

Share This Page