...

View Full Version : Are you tired of document.write() statements cluttering up your code?



ca_redwards
11-05-2004, 03:34 AM
Here's a nice trick I use for writing any string into the current document.


"Look Ma! No hands!".write();

Not valid syntax you say? Let's do this first...


String.prototype.write=Array.prototype.write=function(){document.write(this);return this};

Now we can write arrays too!


[1,2,3].write();

And for the status line...


String.prototype.status=Array.prototype.status=function(){window.status=this;return this};

We can do the same!


[1,2,3].status();

When developing a script, I like to see my intermediary values...


String.prototype.alert=Array.prototype.alert=function(){alert(this);return this};

Now what was that first form trying to do?


document.forms[0].action.alert();

And I can chain these together...


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 (http://www.freewebs.com/ca_redwards/html.js). And you can use my new cross-browser HTML() Construction Kit (http://www.freewebs.com/ca_redwards/html.html) to build complex HTML constructs in 100% pure JavaScript. Enjoy!

Kor
11-05-2004, 09:47 AM
Nice :thumbsup: But...anyway, I never use document.write() method. There are always DOM substitutes for that...

jbot
11-05-2004, 10:32 AM
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 ...


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. :D

Kor
11-05-2004, 12:11 PM
I see your point but... :D 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.

jbot
11-05-2004, 12:20 PM
I see your point but... :D 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 ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum