Any alternative to onload?

N

nntp

Is there anyway to do something exactly like onload, but without the word
onload?

I am trying to write inline js without onload, so I don't know how to
trigger/start the script.
 
N

nntp

Is there anyway to do something exactly like onload, but without the word
onload?

I am trying to write inline js without onload, so I don't know how to
trigger/start the script.

By the way, onmouseover, onblur are all ok. But they require the mouse to
move to the object. Is there a way to make the script run 100%? BODY tag is
not allowed either, so I have to either use a huge table for onmouseover. Is
there any event like onmousemove?
 
F

Fred Oz

You can include the script in the head of your document and kick it off
without a "function" statement - a trivial example below:

<html>
<head><title> Auto-alert </title>
<script type="text/javascript">
alert('Hi from the head')
</script>
</head>
<body onload="alert('Hi from onload');">
Here is the body
</body>
</html>

Of course, instead of popping up an alert, you can call a function or
whatever. Just remember that the script will execute when the browser
reaches it and *will not* wait for the rest of the page to load.

In the example, you should have a blank page until you dismiss the
first alert, then the rest of the page is loaded, then the 'onload'
alert is triggered. So if you are going to manipulate elements, put
the script right at the bottom. Note also that images will likely not
be loaded when your script loads and the user may have already clicked
something before it executes, so it doesn't because the browser has
already headed off to a new location...

Still want to do it? Onload was created for a purpose.

Cheers, Rob.
 
F

Fred Oz

nntp said:
By the way, onmouseover, onblur are all ok. But they require the mouse to
move to the object. Is there a way to make the script run 100%? BODY tag is
not allowed either, so I have to either use a huge table for onmouseover. Is
there any event like onmousemove?

I think it best you explain what you are trying to achieve. How can
you have a table if you don't have a body? You can execute scripts by
just putting commands inside <script> tags, a trivial example is below.

Note that the script executes when the browser reaches it, not when it
has actually rendered the page. Onload was created to run *after* the
page is fully loaded. In the example, the alert in the header runs
before the <body> tag is reached (or the </head> tag for that matter).

The alert in the body will likely run before the page is rendered too,
certainly before images have finished loading, and finally, after the
page is loaded and rendered, the onload will run.

Since you don't want to have a body tag, I presume you are writing the
entire page using JavaScript, which seems quite pointless to me, but if
that's what you want to do have a search for the thread on "writing
slabs of HTML", it should be useful. There is a second example below.

Cheers, Fred.


*Example I:*

<html><head><title> Auto Run </title>

<script type="text/javascript">
doStuff();

function doStuff() {
alert('doStuff\(\) says hello');
}
</script>
</head>
<body onload="alert('Hi from onload');"
style="background-color: #eeeeff;">
<h2>Here is the body</h2>
<script type="text/javascript">
alert('Here I am in the body');
</script>
</body>
</html>



*Example II:*

<html><head><title> Auto Run </title>
</head>
<script type="text/javascript">

var a = [
'<body><h2>here is some text<\/h2>',
'<form action="">',
'<input type="button" value="Click me" ',
'onclick="alert(\'Hi guys\');">',
'<\/form>',
'<\/body>',
];
document.write(a.join(""));
document.close();
</script>
</html>
 
F

Fred Oz

nntp said:
By the way, onmouseover, onblur are all ok. But they require the mouse to
move to the object. Is there a way to make the script run 100%? BODY tag is
not allowed either, so I have to either use a huge table for onmouseover. Is
there any event like onmousemove?

I think it best you explain what you are trying to achieve. How can
you have a table if you don't have a body? You can execute scripts by
just putting commands inside <script> tags, a trivial example is below.

Note that the script executes when the browser reaches it, not when it
has actually rendered the page. Onload was created to run *after* the
page is fully loaded. In the example, the alert in the header runs
before the <body> tag is reached (or the </head> tag for that matter).

The alert in the body will likely run before the page is rendered too,
certainly before images have finished loading, and finally, after the
page is loaded and rendered, the onload will run.

Since you don't want to have a body tag, I presume you are writing the
entire page using JavaScript, which seems quite pointless to me, but if
that's what you want to do have a search for the thread on "writing
slabs of HTML", it should be useful. There is a second example below.

Cheers, Fred.


*Example I:*

<html><head><title> Auto Run </title>

<script type="text/javascript">
doStuff();

function doStuff() {
alert('doStuff\(\) says hello');
}
</script>
</head>
<body onload="alert('Hi from onload');"
style="background-color: #eeeeff;">
<h2>Here is the body</h2>
<script type="text/javascript">
alert('Here I am in the body');
</script>
</body>
</html>



*Example II:*

<html><head><title> Auto Run </title>
</head>
<script type="text/javascript">

var a = [
'<body><h2>here is some text<\/h2>',
'<form action="">',
'<input type="button" value="Click me" ',
'onclick="alert(\'Hi guys\');">',
'<\/form>',
'<\/body>',
];
document.write(a.join(""));
document.close();
</script>
</html>
 
N

nntp

<body> tag is not allowed
<table> is ok

onload is not allowed
onmouseover is ok.

I got it myself
I used <div> + onmouseover
it is almost equal to body+onload, except when the mouse is not on the
screen.

Thanks.
 
L

Lee

nntp said:
<body> tag is not allowed
<table> is ok

onload is not allowed
onmouseover is ok.

When you're asking for a way to work around some limitation
(eg, no body tag), it's a really good idea to explain why you
face that limitation. If you don't, people are going to waste
time asking, anyway, and you probably won't get as much help.

I've had some experiences in which the "page" I was writing
was actually going to be incorporated into a standard <body>
by server-side code, so that's what I'm picturing here.
 
E

Eric Bohlman

Lee said:
When you're asking for a way to work around some limitation
(eg, no body tag), it's a really good idea to explain why you
face that limitation. If you don't, people are going to waste
time asking, anyway, and you probably won't get as much help.

And some people will jump to the conclusion that you're a student asking
other people to do his homework for him.
 
R

Randy Webb

nntp said:
<body> tag is not allowed
<table> is ok

onload is not allowed
onmouseover is ok.

I got it myself
I used <div> + onmouseover
it is almost equal to body+onload, except when the mouse is not on the
screen.

Thats not even close to true. The body/windows onload will only fire
once until the page is reloaded. onmouseover will fire repeatedly if the
mouse is moved in/out of the div. That makes a *huge* difference.

But the whole question almost sounds like homework.
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top