setTimeout function problem

D

David Mark

This time, David Mark's style :)

Not by a longshot.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>untitled</title>
  <meta name="generator" content="TextMatehttp://macromates.com/">
  <meta name="author" content="jorge">
See.

  <!-- Date: 2008-11-29 -->

  <script type="text/javascript">
    self.window.onload= function () {

What sort of nonsense is that?
      function toggleDisplay (p) {
        p.style.display= p.style.display ? '' : 'none';
      }

      window.window.document.getElementById('row2').style.display=
"none";

That's just plain ignorant.

[snip]
 
D

David Mark

Better still:

<script type="text/javascript">
  (function () { return this; })().onload= function () {

That's silly for obvious reasons.
    function window () { return this; };

And that is broken in many UA's (will return the body element.) The
rest is gibberish. Don't post "examples" like this here.
 
S

SAM

Le 11/29/08 6:15 PM, SamuelXiao a écrit :
Thanks, your code is exactly what I want to make. I am now trying to
make it fit my page. And thanks for all other's help!

you delete the class in the 1st sub-div
and you declare in the JS
var num = 1;
 
D

David Mark

And that is broken in many UA's (will return the body element.)

Actually, I misread that part. What I was trying to say is that
sometimes "this" is the body element when using window.onload (or
this.onload.) So never do this:

window.onload = function() {
var doc = this.document; // BAD
};

Do this:

var WIN = this.window;

win.onload = function() {
var doc = WIN.document;
};

If the script is intended to run in environments other than browsers,
you might do:

var GLOBAL = this.window || this;

GLOBAL.onload = function() {
var doc = GLOBAL.document;
};

And, of course, it makes more sense to use the onload attribute of the
body. That is the one bit of script that you do not want to separate
from the markup.
 
D

David Mark

Assuming that this other non-browser environment supports the DOM0 event
interface, can handle the load event on the global object, and has
document as a global property...

No need to assume anything. If it does not support a global onload
property, nothing will happen. If there is no document:

var GLOBAL = this.window || this;

GLOBAL.onload = function() {
var doc = GLOBAL.document;

if (doc) {
if (GLOBAL.alert) { // Simplified--use isHostMethod
GLOBAL.alert('I have a document and I can talk.');
if (GLOBAL.setTimeout) {
GLOBAL.alert('I can set a timer too.');
}
}
}
};
 
D

David Mark

No need to assume anything.  If it does not support a global onload
property, nothing will happen.  If there is no document: [snip]

I didn't mean it would lead to a runtime error - I know you're very
careful in this regard. I just thought it was a rather unlikely
scenario; none of the non-browser environments I've used so far support
either onload or GLOBAL.document. I don't think it would be very
practical to write scripts that use DOM features in an environment
agnostic way. It's probably best to stick to pure ECMAScript features
for such scripts.

That's why there are two examples. If you are writing for browsers
only, then the first one is slightly more efficient.

Methods like setTimeout and alert are not part of the DOM. As for a
document, I don't know what sort of agent would implement a DOM
without a window object, but it doesn't mean there aren't any.
Imagine a console spell checker or validation tool.
 
D

David Mark

Didn't you forget a few "window.window.window"s? And don't forget to
check if window exists, and if it's what you expect it to be. The body
may not be available yet, and the innerHTML property may not be

If it is going to be available at all, it will be available during the
load event.
supported. Come on, you could easily blow this up to at least 30 lines;
don't get lazy now.

Code like that is incompetent and the idea that it would take 30 lines
to make it competent is ridiculous.

var GLOBAL = this.window || this;

WIN.onload = function() {
var body, doc = GLOBAL.document;
if (doc) {
body = doc.body;
if (body && typeof body.innerHTML == 'string') {
body.innerHTML = '<h1>Was that so hard?</h1>';
}
}
};
 
D

David Mark

If it is going to be available at all, it will be available during the
load event.


Code like that is incompetent and the idea that it would take 30 lines
to make it competent is ridiculous.

var GLOBAL = this.window || this;

WIN.onload = function() {

GLOBAL.onload obviously.
 
D

David Mark

GLOBAL.onload obviously.

And I see Jorge's "example" is meant for frames.

var GLOBAL = this.window || this;

GLOBAL.onload = function() {
var body, doc = (GLOBAL.top || GLOBAL).document;
if (doc) {
body = doc.body;
if (body && typeof body.innerHTML == 'string') {
body.innerHTML = '<h1>Was that so hard?</h1>';
}
}
};
 
T

Thomas 'PointedEars' Lahn

Doug said:
I meant what I said. Said what I meant.

What is a "boos", then? I couldn't find it in my dictionaries.
An elephant is faithful 100%.

I don't follow.
Better? Shit, there is always something better. Sometimes good enough
is good enough.

Good enough for you, maybe.
The majority of the content is accurate.

Well, I have only referred to *one paragraph* of *one document* of the
resource you want to recommend as reference. It contained 5 errors and
omissions so far. I leave it to you to compute the percentage of false or
misleading information, knowing of other examples that contain not less
errors and omissions, and that compared that to the reference material
I use and recommend the number of inaccuracies are quite high.
I know exactly what I'm taking about.
Hardly.

I know so much of what I am talking about that I knew you were going
to quibble over my use of the term 'function prototype'.

You may invent terminology as you wish, but don't expect it to be
universally understood.
I ignore your recommendations at not peril whatsoever.

The sad thing about it is that you are in no position to make such an
assessment. Ignorance must be bliss.


PointedEars
 
S

SAM

Le 11/30/08 9:40 PM, Thomas 'PointedEars' Lahn a écrit :
Yes, they are.

Finally, what is this DOM ?
I thought it was a part added to JS 1.2 (or less or more ?).

JS has always been based on Objects.
DOM whom I think is a representation of html document tree,
image ignored by precedent versions of JS,
how can say that anterior features are part of the DOM ?

That's flight! theft of property!
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top