SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Override document.write() with wrapper

    I'm having trouble with this code:
    Code JavaScript:
    var base_document_write = document.write;
     
    document.write = function(html) {
       base_document_write('wrapped: ' + html);
    }
     
    document.write('test');

    I'm trying to get the last line to write "wrapped: test" but instead it does nothing. Firebug reports no errors either.

    I'm not sure what's wrong. Any advice is much appreciated.

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    document.write is a bit fussy about context. Try and force the context.
    Code:
    var base_document_write = document.write;
       
    document.write = function(html) {
       base_document_write.call(document, 'wrapped: ' + html);
    }
     
    document.write('test');

  3. #3
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that works great for FF3.

    The original code worked in IE but not FF. Your version works perfectly in FF but IE7 now says "Object doesn't support this property or method".

    Is the best option going to be browser sniffing?

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,807
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Browser sniffing is always the WORST option.

    If you need to have different code depending on whether a feature is supported or not then test if the feature is supported directly.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How would I do that in the example posted?

    I think I've got it:
    Code JavaScript:
       base_document_write = document.write;
     
       document.write = function(html) {
          try {
             // Firefox (tested in 3)
             base_document_write.call(document, parseHTML(html));
          } catch (e) {
             try {
                // Internet Explorer (tested in 7)
                base_document_write(parseHTML(html));
             } catch (e) {
                // Any others?
                document.body.innerHTML += parseHTML(html);
             }
          }
       }

    But when I was trying the middle version in FF, there was no error or anything so presumably the code in the final catch will never run?
    Last edited by startledferret; Jul 27, 2008 at 13:55.

  6. #6
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Context is the problem. write is normally a method of the document object and IE is upset that it's been moved to another object. Why not just keep the old version attached to document as well?
    Code:
    document.base_document_write = document.write;
       
    document.write = function(html) {
       document.base_document_write('wrapped: ' + html);
    }
     
    document.write('test');
    Now FF and Safari shrug and say "whatever" and IE beams with sudden understanding.

  7. #7
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, good point. Thanks for the help!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •