PDA

View Full Version : Dialogue boxes help


stophon4
03-04-2004, 11:07 PM
I am sorry if that was a bad subject. I have a few questions about prompt boxes, alerts, and confirmations. I browsed the web for them, but I couldn't find the answer so I came here.
Here are the questoins:

1. Is it possible to create your own buttons to click on the alerts or prompts, or any other dialogue box. So on confirmation boxes the choices could be "Yes, No, or cancel"

2. Is it possible to do those in forms, or in a way without dialogue boxes

lavalamp
03-05-2004, 12:51 AM
It wasn't a bad thread title so don't worry about it.

In answer to your first question, check out this (http://forums.webdeveloper.com/showthread.php?s=&threadid=29153) thread, that should explain what you want to know.

For your second question, in forms you can create your own buttons, so you can use them to display what every you want and perform various finctions onclick with JavaScript. So the short answer would be yes.



<form action="#">

<input type="button" value="Yes" onclick="alert('Yes');">
<input type="button" value="No" onclick="alert('No');">
<input type="button" value="Cancel" onclick="alert('Cancel');">

</form>

Kor
03-05-2004, 10:50 AM
Yes, you will find there a nice VBscript/javascript solution, yet don't forget that I think VBscript means IE only....

The perfect cross-browser solution remains, I think, to build your own graphical buttons using Photoshop, Corel or another good graph tool.

stophon4
03-05-2004, 12:27 PM
aright, I am knew to JavaScript, How do you import the buttons?
and can you choose their function? and on more:
Can you bookmark JS?

sorry, My JavaScript book stinks:(

lavalamp
03-05-2004, 05:04 PM
Originally posted by stophon4
How do you import the buttons?To "import" the buttons just copy and paste the HTML code into your page. Simple as that.

Originally posted by stophon4
can you choose their function?
Of course you can choose the JavaScript functions that are run onclick, otherwise what would the point of them be?

Originally posted by stophon4
Can you bookmark JS?
I have no idea what you mean here.

stophon4
03-05-2004, 07:57 PM
well, in html you can hyperlink to some where on the page. I was wondering if you could do that with JavaScript to.

But during all this I forgot the question that I wanted to know more than anything.
here is an example of some code and what I want to do with it:
var age=prompt("What is your age?", "some random age")
if(age=='5')
{
alert("your a kid")
}

I want the alert to pop up if they enter a age 5-9, How do I do that?

(I know that JS was stupid, it was just a example)

lavalamp
03-05-2004, 09:19 PM
Originally posted by stophon4
well, in html you can hyperlink to some where on the pageThat's what anchor tags are for though, there is no need to use JavaScript to perform such a task, especially as approximately 13% of the web-browsing public don't have JavaScript enabled.

Here's a fairly heavy duty script that should do what you want for the age thing:



var age=prompt("What is your age?", "some random age");

while(!age || age.replace(/\D/g,"")==""){age=prompt("What is your age?", "some random age");}

age=parseInt(age.replace(/\D/g,""));

if(age>=5 && age<=9){alert("You're a kid.");}

The while loop makes sure that the user enters something that has at least one number in it. (The !age makes sure that they didn't just press cancel and the .replace removes non-numbers and then makes sure that there is something left.)

The age=parseInt(age.replace(/\D/g,"")); removes any non-numbers and converts the string into a number for use later on.

The if() should be fairly self explanatory.

stophon4
03-05-2004, 10:12 PM
thanks :)

lavalamp
03-05-2004, 10:15 PM
Happy to help. :)

stophon4
03-05-2004, 10:50 PM
aright, here is the actual code I am using and it isn't working:
var score=prompt("What is you highscore for icy-tower?", "somerandomnumber")
while(!score || score.replace(/\D/g,"")==""){score=prompt("What is your age?", "some random age")
score=parseInt(score.replace(/\D/g,""));
if(score>=0 && <=4999)

lavalamp
03-05-2004, 11:06 PM
Always check your code:



var score=prompt("What is you highscore for icy-tower?", "somerandomnumber")
while(!score || score.replace(/\D/g,"")==""){score=prompt("What is your age?", "some random age")
score=parseInt(score.replace(/\D/g,""));
if(score>=0 && <=4999)

You've missed off a closing } for the while loop, you've missed off the word "score" before the <=4999 and of course, you're not running anything with the if statement, (you also didn't change the text in the second prompt). The code should look like this:



var score=prompt("What is your highscore for icy-tower?", "somerandomnumber");

while(!score || score.replace(/\D/g,"")==""){score=prompt("What is your highscore for icy-tower?", "somerandomnumber");}

score=parseInt(score.replace(/\D/g,""));

if(score<=4999){alert();}

I have removed the score>=0 since the replace(/\D/g,"") would remove the minus sign from any numbers that are negative.

sad69
03-05-2004, 11:08 PM
What part of it isn't working? Like what IS happening?

Firstly I can see that you're missing a curly brace.

Your code should read:

var score=prompt("What is you highscore for icy-tower?", "somerandomnumber");
while(!score || score.replace(/\D/g,"")=="") {
score=prompt("What is your age?", "some random age");
}
score=parseInt(score.replace(/\D/g,""));
if(score>=0 && <=4999) {
alert(...);
}


At least I think that's what it should read, syntactically...

If you can clear up what doesn't work, then maybe someone can be of more help.

EDIT: Whoa looks like I was beat to the post! We've said the same thing... though lavalamp had some more pointers

Sadiq.

stophon4
03-08-2004, 09:07 PM
how do you loop JavaScript? just wondering, thanks :)

sad69
03-08-2004, 10:47 PM
Loop Javascript?

Well there are 3 different loops in Javascript:
1. for loop
2. while loop
3. do while loop

Check out this link for more information:
http://www.really-fine.com/javascript_loops.html

There is also a "foreach" type of loop in Javascript as well. It's the for loop, but it takes different "parameters". But I forget how it looks like. I think I used it once, but I can't remember how it goes.

Hope that helps,
Sadiq.

stophon4
03-09-2004, 08:16 PM
I mean for alerts, so I could make an alert appear two or three times.

lavalamp
03-09-2004, 11:28 PM
If you want to produce an alert 3 times then do this:



for(var n=0;n<3;n++){alert("Isn't this really annoying? The web-master should be shot!!!");}

Did you spot the hidden message in there? ;)

sad69
03-10-2004, 05:29 PM
Yes, so you'd use a "for" loop in that case:


for(var i = 0; i < 2; i++) {
alert(...);
}


Change the 2 to a 3 to make it loop 3 times.

EDIT: Sorry lavalamp, I didn't see the second page there... nice hidden message though! lol

Sadiq.

lavalamp
03-10-2004, 11:48 PM
Originally posted by sad69
Sorry lavalamp, I didn't see the second page thereNp :)

Originally posted by sad69
Sorry lavalamp, I didn't see the second page there... nice hidden message though! lolThanx. ;)

stophon4
03-11-2004, 10:46 PM
yeah, I won't use it, but If I felt like it I could loop it 9,00000000000000000000000 and it is 250 alerts long:D

I don't know what's wrong with this code though:
<SCRIPT LANGUAGE="JavaScript">
function Login2()
{
document.write('<b>Username:</b><br><INPUT type=text Name="username" >')
document.write('<br><b>Password:</b><br><INPUT type=text Name="password" >')
document.write('<br><INPUT type=button Name="username" Value="Login" onclick="Login()">')
}

function Login()
{
if(username.value=='smiley_city')
{
if(password.value=='smile')
{
alert("You are logged in!")
document.write('<body bgcolor=orange>')
document.write('<p align="center"><b><font size=6>March 10th, 2003</font><br>[my message]</b></p>')
document.write('[my message]')
document.write('<p align="center"><b><font size=6>March 11th</font><br>Well, email me already, smiley_city@hotmail.com! Your to slow!</b></p>')
}
else
{
alert("You have a invalid password!")
}
}
}

</SCRIPT>

lavalamp
03-11-2004, 10:55 PM
I'm guessing that you are running at least one of these function after the page has loaded. You can't use document.write on a page after it has loaded, simple as that. You can use innerHTML though, if you have a div tag like this one here:

<div id="content_in_here"></div>

Then you can write content into it like this:

document.getElementById("content_in_here").innerHTML="Hello"
document.getElementById("content_in_here").innerHTML+=" there."

stophon4
03-11-2004, 11:05 PM
well I think I am using both of them after the page has loaded, and on of them works, one doesn't, That was probably the problem though, and I don't understand you post above very well, can you make it clearer?

lavalamp
03-12-2004, 12:09 AM
Very simple, use document.write before the page has finished loading so that what you write is included into the source.
Use innerHTML after the page has finished loading if you need dynamic content such as a clock.

If you want to use innerHTML you have to dynamically write the content inside another element. The most common way this is done is to give the container tag a unique ID which can then be referenced like this:

document.getElementById("the_element").innerHTML="This is dynamically created content!!!";

Here is an example of a script using innerHTML:

stophon4
03-12-2004, 08:00 PM
I see what you are showing me but it won't help me, I need you to help me a little bit, how would I make a button on the bottem of the screen called "Member's Login" and it takes you to a Login screen with a username and password input boxes, then JS checks your password and depending on what you enter it puts the text I want to apear on the screen kind of like it is on my site right now (www.smiley-city.tk) except it needs to be harder to hack and it the username and password inputs need to be on the scree. I have tried over and over but I always get errors, please help.:)

lavalamp
03-13-2004, 12:10 AM
Do you want it to be completely unhackable or just quite tricky? I can do both (although nothing is ever really completely unhackable, they could just brute force your server).

stophon4
03-13-2004, 12:29 PM
well, If it is possible completely unhackable, I didn't know that was possible, Thankyou very much!

lavalamp
03-13-2004, 02:52 PM
This is about as secure as it gets with client-side JavaScript. If the username and password were "hello" and "there" then the user would be taken to "hellothere.html". Simply name your target page accordingly and only those who know the name of it can get there.

stophon4
03-13-2004, 05:38 PM
oh, that is good but it doesn't do what I want, I need the whole thing to be programmed into the home page. Will your tricky idea work that way?

lavalamp
03-13-2004, 07:56 PM
It is possible but that means that you would have to write all of your content in document.write() that's a little bit tedious. It also means that the content on your site is inaccessible to those who don't have JavaScript.
It would be better if you were to use a server-side solution to this problem, I could readily write you an ASP script to do this. I'm in the middle of composing a JavaScript script to do this right now but It isn't finished yet, I'll post it when it is.

stophon4
03-13-2004, 08:26 PM
Ok, thanks :thumbsup:

stophon4
03-20-2004, 01:27 AM
I just thought of something, wouldn't the most secure login be that you could only get login if you had the right ip?

P.S. How do you find the users ip?

lavalamp
03-20-2004, 02:13 AM
Sorry I didn't reply, I couldn't find the thread again once I'd finished. Here is a pretty much totally secure JavaScript login system. The username is "hello" and the password is "there" (together they form the name of a .js file that you should put your content in).

I don't know how to get someone's IP address in client-side JavaScript, again I could quite easily show you in ASP though.

There would be no point detecting someone's IP client-side anyway, you wouldn't be able to store it on the server without some sort of server-side language. Having a secure login doesn't neccessarily depend on having the right IP (although it could make it considerably more difficult to break in) but even a simple username and password script (like this one) can be nearly impossible to crack as well.

stophon4
03-20-2004, 01:06 PM
Yes, that looks pretty secure! And I got another Idea, could you disable veiw source and save save as on the context menus?

lavalamp
03-21-2004, 01:27 AM
http://forums.webdeveloper.com/showthread.php?s=&threadid=26841