473,395 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Death Slaught
1,137 1GB
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
Oct 9 '07 #1
12 1636
gits
5,390 Expert Mod 4TB
simply add a

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

kind regards
Oct 10 '07 #2
Death Slaught
1,137 1GB
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
Oct 10 '07 #3
gits
5,390 Expert Mod 4TB
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
Oct 10 '07 #4
Death Slaught
1,137 1GB
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
Oct 11 '07 #5
gits
5,390 Expert Mod 4TB
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
Oct 11 '07 #6
Death Slaught
1,137 1GB
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)
Oct 11 '07 #7
gits
5,390 Expert Mod 4TB
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
Oct 11 '07 #8
Death Slaught
1,137 1GB
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
Oct 11 '07 #9
gits
5,390 Expert Mod 4TB
no problem :) ... post back anytime you have more questions ...
Oct 11 '07 #10
Death Slaught
1,137 1GB
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
Oct 12 '07 #11
gits
5,390 Expert Mod 4TB
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
Oct 12 '07 #12
Death Slaught
1,137 1GB
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
Oct 12 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
10
by: Ken VdB | last post by:
Hi everyone, Is there a reason why the Mid() function only works in one direction in VBScript? This code works in VB6 but not in VBScript? Is there a way around it? I am trying to create an...
12
by: Surya Kiran | last post by:
Hi all, I've written a function template. say template <class T> fn (T var) { ... } Is there any way, from within the function, can we check what type of argument we've passed on to the...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
9
by: Marek Lewczuk | last post by:
Hello, I'm moving out from MySQL to PostgreSQL and there are some function which are not supported in PG so I'm trying to write my own functions. Currently I have big problem with function IF(),...
4
by: Andy_Khosravi | last post by:
Hello, I'm having a problem with the MID function within Access 97. I have been trying to build a function to check to make sure that a field on a form does not have any spaces or dashes. This...
6
by: D | last post by:
Hello all...I have an issue with one of my java script functions that I'm hoping someone can easily help with. I have a web based application that we use to create/sign up for overtime. When we...
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
2
by: Julien | last post by:
Hello all, I would like to do something like: def called(arg) if arg==True: !!magic!!caller.return 1 def caller(arg) called(arg)
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.