SRL Archive - Web Development

JavaScript


sm321

2012-09-21 10:26:28

Hello :) I'm looking to create a JavaScript function of sorts that does this:

Code from www.w3schools.com, it's not my own

(time<10)
{
x="Good morning";
}
else if (time<20)
{
x="Good day";
}
else
{
x="Good evening";
}

I will add the JavaScript tags. I would like it to when the user visits the page the function "plays". How would I go about this please?

Thankyou.

x[Warrior]x3500

2012-09-21 12:37:25

$(document).ready(function () {}

that might just be for Jquery. but either way, i would recommend you use it.

Homepage - http://jquery.com/
Decent beginner tut - http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery

masterBB

2012-09-21 13:27:40

Created something for you. Don't use jquery, it is very bad and complete shit. Don't use javascript either, it is also very bad and also quite shitty.

http://ancientbb.com/tadatada.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Your test page</title>
</head>

<body>
<div id="testDiv">
hallo
</div>
<script src="/yourScript.js" type="text/javascript"></script>
</body>

</html>


document.getElementById("testDiv").innerHTML = 'bye';

var timer = 0;

function timerFunc() {
timer = timer + 1;
var x = "error";

if(timer<10)
{
x="Good morning";
}
else if (timer<20)
{
x="Good day";
}
else
{
x="Good evening";
}

document.getElementById("testDiv").innerHTML = 'tada: ' + x;

setTimeout('timerFunc()',100); // wait 100 ms before recalling this function
}

timerFunc();

e:
SetInterval would be a nicer solution.

sm321

2012-09-21 14:07:18

From: x[Warrior]x3500
$(document).ready(function () {}

that might just be for Jquery. but either way, i would recommend you use it.

Homepage - http://jquery.com/
Decent beginner tut - http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery


From: masterBB
Created something for you. Don't use jquery, it is very bad and complete shit. Don't use javascript either, it is also very bad and also quite shitty.

http://ancientbb.com/tadatada.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Your test page</title>
</head>

<body>
<div id="testDiv">
hallo
</div>
<script src="/yourScript.js" type="text/javascript"></script>
</body>

</html>


document.getElementById("testDiv").innerHTML = 'bye';

var timer = 0;

function timerFunc() {
timer = timer + 1;
var x = "error";

if(timer<10)
{
x="Good morning";
}
else if (timer<20)
{
x="Good day";
}
else
{
x="Good evening";
}

document.getElementById("testDiv").innerHTML = 'tada: ' + x;

setTimeout('timerFunc()',100); // wait 100 ms before recalling this function
}

timerFunc();

e:
SetInterval would be a nicer solution.


Thankyou for the replies :) I'll have a look at the tutorial when I have time. And masterBB, could you make the folder then upload it so I can see a working example please? Because I can't get it to work for some reason (I don't know any JS or jQuery or anything like that.

masterBB

2012-09-21 14:34:53

I posted the working example. It just that link.