views:

45

answers:

2

Hello,

i have this chatbox, and when users login i want it to send a message like this:

Admin: User is online

I really want to use a javascript code for this, because i can work with it quite good. I already have some example, but the problem here is that it does not do something until it is called to do it's function. THATS what i want to achieve, just that when the page is opened up, it does that function one time.

function onLine(message)
{
document.writeform.bericht.value+=message;
document.writeform.bericht.focus();
write1();
}

explanation: onLine is the function and (message) tells what it should say. For example:

...
onclick="onLine('User is online');"
...

the function write1() sends it.

HELP?!

A: 

It sounds like what you're really asking is "how can I cause a particular javascript function to be called when a page loads".

If that's correct:

If you're using jQuery, which I recommend, then you can just do

$(document).ready( function() {
    onLine("User is online");
});

Without jQuery, you can assign a method to the body's onLoad event:

But the body.onload approach can come into conflict with other actions that may be assigned to that event; the jquery approach is safer.

JacobM
where do i have to put that jQuery code? just in my head's javascript?
YourComputerHelpZ
Sure, or in a script tag anywhere on the page.
JacobM
+1  A: 

Do you want to run that function once when the page is loaded? If so, you have many possible ways to do it. A couple of simple ones:

<body onload="onLine('User is online')">

This will call the function when all the page components are loaded. You could also add this to the end of the page:

<script type="text/javascript">
  onLine('User is online');
</script>

There are also javascript libraries like jQuery and prototype, which you should have a look at if you haven't yet.

Kaivosukeltaja