469,336 Members | 5,539 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

Any alternative to onload?

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.
Jul 23 '05 #1
9 18882
> 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?
Jul 23 '05 #2
nntp wrote:
Is there anyway to do something exactly like onload, but without the word
onload?


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.
Jul 23 '05 #3
nntp wrote:
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?


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>

Jul 23 '05 #4
nntp wrote:
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?


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>

Jul 23 '05 #5
*nntp* wrote:
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.


Sorry, the problem you describe is not very clear. I suspect you mean
that you can't add the onload attribute to your body tag (for various
reasons), but nevertheless require an onload ability? In which case the
script: window.onload = MyFunction; would probably do the job for you.

Then again you mention 'write inline script' and this could imply the
use of document.write() - if so you likely won't want to be executing
that after the document has been loaded.

Anyhow, not knowing what you really want, here's a snippet of script
that attempts to execute function MyFunction() after the document has
loaded and tries not to use "onload" unless it's the only available
method of invoking functions after the document has loaded:
if (window.attachEvent) {
window.attachEvent('onload', MyFunction);
}
else if (window.addEventListener) {
window.addEventListener("load", MyFunction, false);
}
else {
window.onload = MyFunction;
}

--
Andrew Urquhart
- FAQ: http://www.jibbering.com/faq/
- Archive: http://groups.google.com/groups?grou...ang.javascript
- Reply: http://andrewu.co.uk/contact/
Jul 23 '05 #6
<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.
Jul 23 '05 #7
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.

Jul 23 '05 #8
Lee <RE**************@cox.net> wrote in news:cl*********@drn.newsguy.com:
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.
Jul 23 '05 #9
nntp wrote:
<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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Peter Cartwright | last post: by
2 posts views Thread by llaxman | last post: by
2 posts views Thread by Jarson | last post: by
5 posts views Thread by joe | last post: by
3 posts views Thread by Vik Rubenfeld | last post: by
7 posts views Thread by Spartanicus | last post: by
15 posts views Thread by Aaron Gray | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.