Connecting Tech Pros Worldwide Forums | Help | Site Map

Stoping function from continueing when if/ if else statement is true.

Death Slaught's Avatar
Site Addict
 
Join Date: Aug 2007
Location: Tennessee
Posts: 952
#1: Oct 9 '07
I got bored in math today and decided to write a "program" that greets the user, and if the user has a certain name it will take them to a site, and if you're not one of the users something different happens:

[HTML]<html>
<head>
<script type="text/javascript">

var fn;
var ln;
var hay;
var cn;
var dy;
var fs;

function greeting()

{

fn=prompt("Hello what's your name?", alt="Your first name")
alert("That's a great name" + " " + fn + "!");

ln=prompt("So" + " " + fn + "," + "what's your last name?", alt="Your last name")
alert("Well it's nice to meet you" + " " + fn + " " + ln + "!")

alert("As you know i'm your computer, but you can give me a name if you want to.")

cn=prompt("So what's my name?", alt="computer")
alert(" " + cn + "," + "I love it!")

hay=prompt("So" + " " + fn + "," + "how are you today?", alt="great")

if (hay == "good")
{
alert("That's great!")
}
else if (hay == "fine")
{
alert("That's great!")
}
else if (hay == "great")
{
alert("That's great!")
}
else if (hay == "awesome")
{
alert("That's great!")
}
else if (hay == "ok")
{
alert("That's great!")
}
else
{
alert("I hope your day goes better!")
}

if (fn == "User1" && ln == "user1")
{
alert("Nice to see you again User1 user1, here's your page!")
top.window.location="http://google.com";
}
else if (fn =="User2" && ln == "user2")
{
alert("Nice to see you again User2 user2, here's your page!")
top.window.location="http://thescripts.com";
}
else
{
alert("It's been great talking with you!")
}

dy=prompt("Do you want to talk with me again?", alt="yes")

if (dy == "yes")
{
greeting();
}
else if (dy == "no")
{
alert("Then let me take you to you favorite site.")
fs=prompt("Please enter your favorite site do not start with http:// or www.")
top.window.location="http://" + fs + " ";
}
else
{
alert("Bye")
}

}

</script>
</head>
<body>
<form>
<input type="button" onclick="greeting()" value="Do you want to talk to me?" />
</form>
</body>
</html>[/HTML]
There's only one problem, if you are one of the selected users, it starts to take you to that site, and then it prompts you for the dy variable, and continues with the if...else if......else after that.

So how can I stop it from continueing if you are one of the users?


Thanks, Death

gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,135
#2: Oct 10 '07

re: Stoping function from continueing when if/ if else statement is true.


simply add a

Expand|Select|Wrap|Line Numbers
  1. return;
after setting the location ...

kind regards
Death Slaught's Avatar
Site Addict
 
Join Date: Aug 2007
Location: Tennessee
Posts: 952
#3: Oct 10 '07

re: Stoping function from continueing when if/ if else statement is true.


Quote:

Originally Posted by gits

simply add a

Expand|Select|Wrap|Line Numbers
  1. return;
after setting the location ...

kind regards

Thanks just what I needed.

I do have another question though.

Is it possible to store the contents of a textarea in a variable when the user clicks a "submit" button. Then put the contents back into different textareas or in a section of text.

Let me extrapolate.

Say you have three textareas. One asks you your favorite color, the other your favorite food, and the last your favorite soda, and there is a submit button at the bottom. When the button is clicked the contents of each textarea are stored in 3 different variables. Then say you have a paragraph, and what the user entered is inserted into certain sections in that paragraph, like making a story based on what the user enters.

I know you can do this with a server side langauge, but how would you go about doing it with JavaScript? Is it even possible?

Thanks, Death
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,135
#4: Oct 10 '07

re: Stoping function from continueing when if/ if else statement is true.


hi ...

of course it is ;) ... let me give you an typical example:

[HTML]<html>
<script type="text/javascript">
function shift_value() {
var input_node = document.getElementById('my_input_id');
var div_node = document.getElementById('my_div');

var div_value = input_node.value;

div_node.innerHTML = '';
div_node.innerHTML = div_value;
}
</script>
<body>
<form action="" name="my_form">
<input type="text" name="my_input" id="my_input_id"/>
<input type="button" value="shift to div" onclick="shift_value();"/>
</form>
<div id="my_div"></div>
</body>
</html>
[/HTML]
kind regards
Death Slaught's Avatar
Site Addict
 
Join Date: Aug 2007
Location: Tennessee
Posts: 952
#5: Oct 11 '07

re: Stoping function from continueing when if/ if else statement is true.


Quote:

Originally Posted by gits

hi ...

of course it is ;) ... let me give you an typical example:

[HTML]<html>
<script type="text/javascript">
function shift_value() {
var input_node = document.getElementById('my_input_id');
var div_node = document.getElementById('my_div');

var div_value = input_node.value;

div_node.innerHTML = '';
div_node.innerHTML = div_value;
}
</script>
<body>
<form action="" name="my_form">
<input type="text" name="my_input" id="my_input_id"/>
<input type="button" value="shift to div" onclick="shift_value();"/>
</form>
<div id="my_div"></div>
</body>
</html>
[/HTML]
kind regards

Thanks it's just what I need.....but how would I have say 3 text inputs, that submited to different variables, with the click of one button. Also how would you insert the contents of each variable into a different section in say a paragraph.

Thanks, Death
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,135
#6: Oct 11 '07

re: Stoping function from continueing when if/ if else statement is true.


hi ...

as you can see in the example ... it works with id's that are assigned to the html-elements. now when using 3 buttons call the function with a param that passes the desired ids to the shift-value function. in it simply use that ids (input-id, output-id) instead of the actually hard-coded ones. in case you want to store the values in a variable you may use globals to do so ... make a try and post some code in case you have problems with it ...

ah ... and of course you may replace the div with a p ... the magic is done with the ids and innerHTML :)

kind regards
Death Slaught's Avatar
Site Addict
 
Join Date: Aug 2007
Location: Tennessee
Posts: 952
#7: Oct 11 '07

re: Stoping function from continueing when if/ if else statement is true.


Quote:

Originally Posted by gits

hi ...

as you can see in the example ... it works with id's that are assigned to the html-elements. now when using 3 buttons call the function with a param that passes the desired ids to the shift-value function. in it simply use that ids (input-id, output-id) instead of the actually hard-coded ones. in case you want to store the values in a variable you may use globals to do so ... make a try and post some code in case you have problems with it ...

ah ... and of course you may replace the div with a p ... the magic is done with the ids and innerHTML :)

kind regards

Thanks again I think I get it, but could you explain the example for me? Like how it knows which variable to store it in, and how it knows where to get the text from. ( I think it's the name attribute ). I don't want you to just give me the answer, but I don't want to have to go and take a tutorial on JavaScript just to learn how to do this, I'm a beginner at Javascript I learn what I need when I need it (Not the greatest idea I know), but if you could break it down a little that would be great.

Thanks gits, Death

PS - Several months ago I wrote a script that changed the background color when you click a button:

[HTML]<html>
<head>
<script type="text/javascript">

function change()

{
document.bgcolor=" #000000";
}

</script>
</head>
<body>
<form>
<input type="button" onclick="change()" value="black" />
</form>
</body>
</html>[/HTML]

It worked back then, but for some reason it's not working any more. Any clue why this is happening? (I probably just messed up in the code some where lol)
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,135
#8: Oct 11 '07

re: Stoping function from continueing when if/ if else statement is true.


i comment the js-code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function shift_value() {
  3.     // here we retrieve a reference to the input-node using its id
  4.     // you may pass it through the function and use the param here
  5.     // for that
  6.     var input_node = document.getElementById('my_input_id');
  7.  
  8.     // here we retrieve a reference to the output-node using its id
  9.     var div_node   = document.getElementById('my_div');
  10.  
  11.     // this is the variable that we use to store the input-node's-value
  12.     // use a global var in case you really want to store it
  13.     var div_value  = input_node.value;
  14.  
  15.     // we clean up the innerHTML of our output node
  16.     div_node.innerHTML = '';
  17.  
  18.     // now we set the innerHTML of the output-node to the input's value
  19.     div_node.innerHTML = div_value;
  20. }
  21.  
kind regards
Death Slaught's Avatar
Site Addict
 
Join Date: Aug 2007
Location: Tennessee
Posts: 952
#9: Oct 11 '07

re: Stoping function from continueing when if/ if else statement is true.


Quote:

Originally Posted by gits

i comment the js-code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function shift_value() {
  3.     // here we retrieve a reference to the input-node using its id
  4.     // you may pass it through the function and use the param here
  5.     // for that
  6.     var input_node = document.getElementById('my_input_id');
  7.  
  8.     // here we retrieve a reference to the output-node using its id
  9.     var div_node   = document.getElementById('my_div');
  10.  
  11.     // this is the variable that we use to store the input-node's-value
  12.     // use a global var in case you really want to store it
  13.     var div_value  = input_node.value;
  14.  
  15.     // we clean up the innerHTML of our output node
  16.     div_node.innerHTML = '';
  17.  
  18.     // now we set the innerHTML of the output-node to the input's value
  19.     div_node.innerHTML = div_value;
  20. }
  21.  
kind regards

Thanks I understand It alot better now.

I have one more question for you then i'll leave you alone for a while XD. ( I'll post it a few min. I'm looking for something to go with it.)

Thanks alot gits, Death
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,135
#10: Oct 11 '07

re: Stoping function from continueing when if/ if else statement is true.


no problem :) ... post back anytime you have more questions ...
Death Slaught's Avatar
Site Addict
 
Join Date: Aug 2007
Location: Tennessee
Posts: 952
#11: Oct 12 '07

re: Stoping function from continueing when if/ if else statement is true.


I posted this yesterday, but It must have gotten deleted........


Like I said in the other post I don't want you to just give me the answer, but talk/type me through it and explain it, so it may be a learning experience.

This is what I need.

Text scrolling from right to left in a limited amount of space.

The text needs to be told when to pause and start again.

I also need to know how to "decorate" the text like change its color.

Finally I need certain words to make the screen shake....something like this but without the buttons.

[HTML]<html>
<head>
<script>
function startEQ()
{
richter=5
parent.moveBy(0,richter)
parent.moveBy(0,-richter)
parent.moveBy(richter,0)
parent.moveBy(-richter,0)
timer=setTimeout("startEQ()",10)
}
function stopEQ()
{
clearTimeout(timer)
}
</script>
</head>
<body>

<form>
<input type="button" onclick="startEQ()" value="Start an earthquake">
<br />
<br />
<input type="button" onclick="stopEQ()" value="Stop the earthquake">
</form>

</body>
</html>[/HTML]

--------------------------------------------------------------------------------------------------------------------

What I need this for is a website with music videos, and I need the scrolling text to be the lyrics to the songs. ( What I need the other stuff for is to make it fancy ).


Thanks alot gits (and any one else who has suggestions), Death
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,135
#12: Oct 12 '07

re: Stoping function from continueing when if/ if else statement is true.


hi ...

it was not deleted ... i splitted it from this thread here since i covers an entire different topic ... you find it here

kind regards
Death Slaught's Avatar
Site Addict
 
Join Date: Aug 2007
Location: Tennessee
Posts: 952
#13: Oct 12 '07

re: Stoping function from continueing when if/ if else statement is true.


Quote:

Originally Posted by gits

hi ...

it was not deleted ... i splitted it from this thread here since i covers an entire different topic ... you find it here

kind regards

O.O lol sorry and thanks.

- Death
Reply