Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2002
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow Are you tired of document.write() statements cluttering up your code?

    Here's a nice trick I use for writing any string into the current document.

    Code:
    "Look Ma!  No hands!".write();
    Not valid syntax you say? Let's do this first...

    Code:
    String.prototype.write=Array.prototype.write=function(){document.write(this);return this};
    Now we can write arrays too!

    Code:
    [1,2,3].write();
    And for the status line...

    Code:
    String.prototype.status=Array.prototype.status=function(){window.status=this;return this};
    We can do the same!

    Code:
    [1,2,3].status();
    When developing a script, I like to see my intermediary values...

    Code:
    String.prototype.alert=Array.prototype.alert=function(){alert(this);return this};
    Now what was that first form trying to do?

    Code:
    document.forms[0].action.alert();
    And I can chain these together...

    Code:
    document.title.link(window.location.href).status().alert().write();
    This builds a link, shows it in the status line, issues it in an alert, and waits until the alert is dismissed before writing it into the document.

    It makes for a nice debugging tool.

    These are actually incorporated into my HTML() Bookmarklet Library. And you can use my new cross-browser HTML() Construction Kit to build complex HTML constructs in 100% pure JavaScript. Enjoy!
    Last edited by ca_redwards; 11-05-2004 at 04:42 AM.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Nice But...anyway, I never use document.write() method. There are always DOM substitutes for that...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor
    I never use document.write() method. There are always DOM substitutes for that.
    document.write() is better for adding elements in some cirumstances, such as writing to an about:blank page ...

    PHP Code:
    with ((popwin=window.open("about:blank","mypopup","width=640,height=480",true).document)
    {
         
    open();
         
    write("<html>","<head>","<title>hello world</title>","</head>","<body>","<h1>Hello World</h1>","</body>","</html>");
         
    close();

    DOM methods would take too much coding in this instance.
    *keep it simple (TM)

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I see your point but... I never write just a 'HelloWorld!' on a blank page... Most of the time I wrote complex tables and nested things which are to be dinamically changed later, so DOM is by far the best choise.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor
    I see your point but... I never write just a 'HelloWorld!' on a blank page. Most of the time I wrote complex tables and nested things which are to be dinamically changed later, so DOM is by far the best choise.
    it's horses for courses. of course, you can write out tables. DOM is only more effecient if you're inserting into a document, otherwise docWrite is preferable.

    "hello world" is just a simple example. thought you might understand that easier
    *keep it simple (TM)


  •  

    Posting Permissions

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