PDA

View Full Version : HELP: onload function for DIV?



matt blank
08-03-2007, 05:51 PM
Hi,

I'm hoping someone can help me and that this will be a fairly simple request.

I want to mimick a DIV being clicked on when a page loads.

Except for presuming it has to be some sort of onload function in the body tag (which I'm not very hot on), I don't know much else!

Can someone help me please? The DIV I want to load (as if it was clicked on) will be called story03.

Thanks!
Matt

jscheuer1
08-03-2007, 06:19 PM
Since a division has no native onclick function, it really would be best to do this onload. Onload events however are often best done somewhere else than in the body tag. Depending upon the onclick function of the division in question and how that gets assigned and what it does, how it does it, what else in the page it does it with, you may or may not be able to fire its event immediately after the browser parses the division. Also, when you click on something, generally the browser can know what element has been clicked, if this is important, running this function by any other method will require informing the browser as to which element it now refers.

To be more specific, I would need to see the page and know just exactly what you want to have happen. If the timing is critical and other page elements and scripts need to be loaded first, the event you wish to fire may need to be delayed longer than you might wish in order to be able to happen at all.

???
08-03-2007, 06:29 PM
If you're wanting to keep all your JS in the head, I'm not sure if it can be done, at least if it starts in the page and isn't created dynamically, if it starts in the page and you don't reall care about where your JS is, you could put a script element at the end of it.
I was going to say try this:


<div id="story03">
<script type="text/javascript">
document.getElementById ("story03").onload = function () {
//Do stuff when it loads...
}
</script>
<!-- Div contents go HERE! --!>
</div>

But it didn't work and I don't really know why. So just do:


<div id="story03">
<!-- Div contents go HERE! --!>
<script type="text/javascript">
//Do stuff when it loads...
</script>
</div>

I think that will work.

jscheuer1
08-03-2007, 06:34 PM
. . . But it didn't work and I don't really know why. So just do:


<div id="story03">
<!-- Div contents go HERE! --!>
<script type="text/javascript">
//Do stuff when it loads...
</script>
</div>

I think that will work.

The first example doesn't work because there is no onload event for division. Even if there were, it wouldn't work if any of the following reasons why your second method could have problems are true. The second will not work unless there is no other initialization for the page or content to be parsed that needs to be accomplished before "//Do stuff when it loads..." can be done.

???
08-03-2007, 06:57 PM
Well could you use addEventListener instead of onload? I didn't sugest that because of IE, and I don't know how you use attachEvent.

jscheuer1
08-03-2007, 07:06 PM
Well could you use addEventListener instead of onload? I didn't sugest that because of IE, and I don't know how you use attachEvent.

No, because there is still no onload event for division, unless you mean to addEvent/attacthEvent onload of the page to fire something for the division. That could be done. It's probably what I would have suggested for the original question in this thread once I knew the particulars. However, there may, in this case be a better way. And since matt blank didn't seem all that comfortable with coding, I figured I'd wait until I saw what matt blank wanted to be done before throwing code around.

matt blank
08-05-2007, 03:38 AM
Hi guys,

Thanks for all the suggestions. I was able to get the effect I waned through a different way actually.

Basically in a nutshell: I'm using something called RSS2HTML which throws out x amount of news stories based on a template. On top of this I'm using the Animated Scrolling DIV from DD to make each story expand.

What I wanted was to have the first one open and the rest closed on load. But becaused it's templae based if I was to set that function to true or false it would have done it to every story. Therefore I thought about the onload function as a way around it, so it would appear someones clicked on the first story and expanded it.

It turns out a friend was able to help me by creating a PHP variable throught the PHP code that comes with RSS2HTML.

Thank you very much for your help anyway.

Matt