PDA

View Full Version : alert box with ok and cancel buttons, when you click ok you get taken to...



Jrd
07-21-2006, 02:34 AM
Hi all,
I need help making an alert box like this:
http://www.imgcrate.com/Jrd/alertbox.png
that pops up when you click a link,and when you click the "ok" button it takes you to the links destination and when you click cancel nothing happens.

Does anyone know how to do this?

jscheuer1
07-21-2006, 04:01 AM
Well, it depends upon what you want to have happen if javascript is disabled. In this version, with javascript disabled, it will act as a normal link:


<a href="http://www.google.com/" onclick="return confirm('Do You Really Want to go to Google?');">Google</a>

With javascript enabled, it will behave just as you asked it to. If the game cannot be used and played without javascript, this is probably the best method. However, if javascript would be required at the destination but not on the originating page or confirmation is required for legal or other reasons, you could do this:


<a href="javascript:void(0);" onclick="theVar=confirm('Do You Really Want to go to Google?');setTimeout('if(theVar){window.location=\'http://www.google.com/\'}', 0);">Google</a><br>
<noscript>Javascript Enabled required for above link</noscript>

jpaulraj
07-21-2006, 03:08 PM
How to change this so that, when you click OK, it initiates an action? (click on delete icon, press OK and it calls the javascript to delete the row) i have the script to delete a row.. how should i call a js function within this confirm syntax?

jscheuer1
07-21-2006, 03:39 PM
Generally, you can just put it in the flow of the delete function. Something like:



function delete(){
do some prep stuff here if you like
var answer=confirm('Do you Really want to do this?')
if (answer) {
do the thing here
}
do some clean up stuff here if needed
}

jpaulraj
07-21-2006, 03:42 PM
thanks jscheuer1

Jrd
07-22-2006, 01:09 AM
Thanks jscheuer1, that would work except I'm using dynamic drives Fold-out external menu (http://www.dynamicdrive.com/dynamicindex1/slideinmenu4.htm) and it dissapears whenever I have an onclick attribute on this link. Here ill post some code so you can see what I'm dealing with here, its php.


<td valign="top"><div align="center">
<?php
for ($i=0; $i <= count($arraymember)-1; $i++){

echo("<a href=\"?world=" . $arraymember[$i] . "&detail=" . $detailq . "\" class=\"world\" target=game >World " . $arraymember[$i] . "</a></ br>");

}
?>
</div></td>

I also tried adding code for the onclick event like this:

$onclick = "onclick="return confirm('Do You Really Want to go to Google?');""
And then using . $onclick . to call it but it didnt like it and it hid the foldout menu.

Anymore ideas?

Heres where its being used:
http://runecore.selfip.com:82/switch/switchindex.php
and heres the link to the menu file:
http://runecore.selfip.com:82/switch/menu.php

Twey
07-22-2006, 01:41 AM
<?php
for ($i=0; $i <= count($arraymember)-1; $i++){

echo("<a href=\"?world=" . $arraymember[$i] . "&detail=" . $detailq . "\" class=\"world\" target=game >World " . $arraymember[$i] . "</a></ br>");

}
?> can be summed up as:
<?php
for ($i=0; $i < count($arraymember); $i++)
echo(
'<a ' .
'href="?world=' . $arraymember[$i] . '&amp;detail=' . $detailq . '" ' .
'class="world" ' .
'target="game" ' .
'onclick="return window.confirm(\'Do You Really Want to go to Google?\');"' .
'>' .
'World ' . $arraymember[$i] .
'</a>' .
'<br>'
);
?>

Jrd
07-22-2006, 01:59 AM
It works this way! Thank you Twey, what was differnet that made it work?

Twey
07-22-2006, 02:05 AM
Most important one was the proper escaping of the quotes :) I tidied up the HTML somewhat too, though.

Jrd
07-22-2006, 02:07 AM
you mean by using single quotes ' instead of double quotes " ?

Twey
07-22-2006, 02:17 AM
$onclick = "onclick="return confirm('Do You Really Want to go to Google?');""You nested double quotes without escaping the inner ones.

An interesting point to note here is that double quotes are significantly slower to parse than single quotes, so single quotes should be used where possible, especially if you've no real use for double quotes as in the code above.

Jrd
07-22-2006, 02:33 AM
Oh, thankyou, I didn't know that.

Could you help me fix up this code?

<?php
for ($i=0; $i <= count($arrayfree)-1; $i++){

echo("<a href=\"?world=" . $arrayfree[$i] . "&detail=" . $detailq . "\" class=\"world\" target=game >World " . $arrayfree[$i] . "</a></ br>");

}
?>
It should be like the other one but it has $arrayfree instead of $arraymember.
I tried to make it like the one you did but it wouldn't work for me.

Twey
07-22-2006, 02:37 AM
Just copy the above code and change all instances of $arraymember to $arrayfree.

Jrd
07-22-2006, 03:31 AM
Just copy the above code and change all instances of $arraymember to $arrayfree.
oh yeah that works too, I've done that and now it works great :) Thankyou Very Much.

And do you know why, when I load a world here http://runecore.selfip.com:82/switch/switchindex.php and then scroll to the left the fold-out menu goes behind the java applet? I am running java version 1.5 I think its the newest but im not sure.

mburt
07-22-2006, 03:13 PM
this one's easy:
<html>
<head>
<script>
function goto(site) {
var msg = confirm("Go to " + site + "?")
if (msg) {window.location.href = site}
else (null)
}
</script>
</head>
<body>
<a href="javascript:goto('http://www.yahoo.com')">Yahoo</a>
</body>
</html>

Twey
07-22-2006, 10:36 PM
Er... that's already been said. The question has been taken to new and more complex levels, a long time ago. :p Oh, and:
if (msg) {window.location.href = site}
else (null)Why'd you use braces for the first condition, but not the second? And why did you use a no-op rather than simply omitting the else keyword altogether?

mburt
07-22-2006, 11:52 PM
no, Twey meant that there is escape characters you have to put before certain characters.
ex:
document.body.innerText = "Hi, my name is \"poo\".\nI eat code for lunch."
the \" makes a " character, and \n makes a new line.

There is alot more escape characters than these. i recommend getting a guide or something with a list.

Twey
07-22-2006, 11:56 PM
And do you know why, when I load a world here http://runecore.selfip.com:82/switch/switchindex.php and then scroll to the left the fold-out menu goes behind the java applet?Embedded plugin content always renders above DHTML content. You can work around this (usually) by putting the plugin content inside an iframe.

Jrd
07-23-2006, 12:49 AM
Embedded plugin content always renders above DHTML content. You can work around this (usually) by putting the plugin content inside an iframe.
Thats what I have done. Is there any other way to make embedded objects go below the DHTML?

Twey
07-23-2006, 12:57 AM
No, not really, to the best of my knowledge.

Jrd
07-23-2006, 06:39 AM
No, not really, to the best of my knowledge.
oh well, maybe ill try some other coding places. maybe some sort of php or something could do it.

Twey
07-23-2006, 01:19 PM
Er, I doubt it. If there were a way, it would be in Javascript and/or CSS. However, I don't think there is, other than the aforementioned iframe trick. Remember to make sure its z-index is lower than that of the higher content.

Of course, some browsers view iframes as embedded content, and display them on top of everything else. This isn't helpful.

mburt
07-23-2006, 01:44 PM
Okay... ignore me completely if I don't make much sense. I'm horrible with stuff like this. :)

Jrd
07-23-2006, 10:24 PM
Er, I doubt it. If there were a way, it would be in Javascript and/or CSS. However, I don't think there is, other than the aforementioned iframe trick. Remember to make sure its z-index is lower than that of the higher content.

Of course, some browsers view iframes as embedded content, and display them on top of everything else. This isn't helpful.
I looked through my code again and I had used z-index on the menu but hadn't used z-index on the iframe so now I set the z-index on the menu to 100 and the z-index on the iframe to 1 and that seem to have fixed it. Thankyou again for your help.