Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Old 04-12-2007, 07:49 PM Mean Alert
Padawan Geek's Avatar
Novice Talker

Posts: 13
Trades: 0
I have two scripts running on the same page, but when I load the page, the one that has the alert in it wont work. When I take the other one out, the alert works fine. Why is this?

Here is the code.

alert:
Code:
window.onload = construction;

function construction() {
    alert("This site is still under construction. Some of the links and effects may not work. Still, you can look around.")
}
other one:
Code:
window.onload = timegreeting;

function timegreeting() {
    var now = new Date();
    var greet;

    if (now.getHours() < 11) {
        greet = "<p>Good morning!</p>";
    }
    
    else if (now.getHours() < 17) {
        greet = "<p>Yo!</p>";
    }

    else {
        greet = "<p>Good evening!</p>";
    }
    
    document.getElementById("greeting").innerHTML = greet;
}
Thanks!
__________________
function signature() {
document.write("I'm a geek! (duh!)")
}


Last edited by Padawan Geek; 04-12-2007 at 09:25 PM..
Padawan Geek is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-13-2007, 12:39 AM Re: Mean Alert
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
You're writing over the window.onload event twice:
Code:
window.onload = construction;
and
Code:
window.onload = timegreeting;
Instead, use the DOM methods (can't remember the ones to add events to the window object) or use a control function. For example,

Code:
window.onload = pageLoader;
function pageLoader() {
  construction();
  timegreeting();
}
The advantage of using DOM (not shown above) is that your code will work with other code that does (but break if it doesn't).
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 04-13-2007, 02:34 AM Re: Mean Alert
jito's Avatar
MY LIFE IS 'i' LIFE

Posts: 565
Name: surajit ray
Location: inside the heart of my friends
Trades: 0
Or attach the second function with body onload inside the page.
__________________
I am not smart, that's why i don't act smart


Please login or register to view this content. Registration is FREE
jito is offline
Reply With Quote
View Public Profile
 
Old 04-13-2007, 10:54 AM Re: Mean Alert
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
an alternative (but, in the same spirit) to Jeremy's suggestion:
Code:
window.onload = function () {
  construction();
  timegreeting();
}
__________________

Please login or register to view this content. Registration is FREE

willcode4beer is offline
Reply With Quote
View Public Profile
 
Old 04-13-2007, 09:01 PM Re: Mean Alert
Padawan Geek's Avatar
Novice Talker

Posts: 13
Trades: 0
Thanks guys, but is there a way to make it get the functions from seperate files, like have it go
Code:
window.onload = function () {
bla bla bla
}
and have it call out to other files that have the scripts in them, so that I can have each thing in it's own file.
__________________
function signature() {
document.write("I'm a geek! (duh!)")
}

Padawan Geek is offline
Reply With Quote
View Public Profile
 
Old 04-13-2007, 10:48 PM Re: Mean Alert
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Well, you don't call other files, you can include them, but that'd be best done using PHP to minimize the overhead of loading the javascript. You could then call all appropriate functions. For example, in a PHP file you may have the following

PHP Code:
 <?php
   
if (whatever you want) {
    echo 
"<script src=\"my_file.js\" type=\"text/javascript\" ></script>\n";
   }
 
?>
window.onload = pageLoader;
function pageLoader() {
  construction();
  timegreeting();
<?php
  
if (whatever you want) {
    echo 
"my_function();\n";
  }
?>
}
Where my_function() is defined in my_file.js
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 04-13-2007, 11:36 PM Re: Mean Alert
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,938
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
You can call both of your functions in a body tag as well using onload.

<body onload="construction();timegreeting();">

Mind you, Jeremy may have given you a piece of good advice without realizing it; you would be far better off to generate the greeting line using server-side HTML. It would work across all browsers and SE spiders and everyone would see it.

Jeremy, I'd give you positive TP for that thought pattern you triggered, but I gotta find other people to give it to first. So I owe you one.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 04-14-2007, 01:06 AM Re: Mean Alert
Padawan Geek's Avatar
Novice Talker

Posts: 13
Trades: 0
Quote:
Originally Posted by JeremyMiller View Post
Well, you don't call other files, you can include them, but that'd be best done using PHP to minimize the overhead of loading the javascript. You could then call all appropriate functions. For example, in a PHP file you may have the following

PHP Code:
 <?php
   
if (whatever you want) {
    echo 
"<script src=\"my_file.js\" type=\"text/javascript\" ></script>\n";
   }
 
?>
window.onload = pageLoader;
function pageLoader() {
  construction();
  timegreeting();
<?php
  
if (whatever you want) {
    echo 
"my_function();\n";
  }
?>
}
Where my_function() is defined in my_file.js
You've lost me ... I have no experience in PHP so I have no idea what to do with that. Eek.
__________________
function signature() {
document.write("I'm a geek! (duh!)")
}

Padawan Geek is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Mean Alert
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.64445 seconds with 12 queries