PDA

View Full Version : Confirm onChange



texasgal
03-30-2006, 05:12 PM
I would like to create a drop down list. And if an item is selected a popup alert window or a confirmation window opens. If they chose ok they will be redirected to a new url. But not all items in the dropdown will have this alert.

So option #1 will have a alert "you are leaving our website. are you sure you want to continue?" if yes, open a new window.
Option #2 has no alert but takes you to a new location in a new window.

does this make sense? I just can't seem to find what I am looking for.

Thanks.

.alias.
03-30-2006, 05:18 PM
OK think this is what you want.

put this in your head

<script language="javascript">
<!--
function pop(question)
{
agree=confirm(question);
if (agree)
{
window.location = "http://www.google.com";
}else
{
alert('Thanks for staying!');
}
}
//-->
</script>

and this is an example


<input type="button" onClick="pop('Are you eure you want to leave?')">

hope it helps

texasgal
03-30-2006, 06:19 PM
Thank you that is really close to what i need, but each option of the drop down will have its unique URL. I was messing with some samples that i found and came up with this:

In the header:

function go_there(targ,selObj,restore)
{
var where_to= confirm("What your leaving! Are you sure? Press OK to continue.");
if (where_to== true)
{
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
}



<FORM>
<SELECT NAME="neighborhood" onChange="go_there('parent',this,0)" >
<OPTION SELECTED="selected" VALUE="">- - Select One- -</OPTION>
<OPTION VALUE="http://www.yahoo.com/)" >Yahoo</OPTION>
<OPTION VALUE="http://www.google.com/">Google</OPTION>
</SELECT>
</FORM>

This seems to work. Do you see anything wrong with it?

texasgal
03-30-2006, 06:25 PM
Actually that isn't going to work because some don't need to have an Alert message. What do i need to add?

.alias.
03-30-2006, 06:31 PM
just add the onChange code to each of the options instead of them main select


<select........> //no on change
<OPTION SELECTED="selected" VALUE="">- - Select One- -</OPTION>
<OPTION onChange="go_there('parent',this,0)" VALUE="http://www.yahoo.com/)" >Yahoo</OPTION>
<OPTION onChange="go_there('parent',this,0)" VALUE="http://www.google.com/">Google</OPTION>
</SELECT>


that might work

Twey
03-30-2006, 06:46 PM
<script type="text/javascript">
function movePage(url) {
var isInternal = function(u) {
return ((u.indexOf("://") == -1 && u.indexOf("www.") == -1) || u.indexOf(window.location.host) > -1);
}

if(isInternal(url))
window.open(url);
else if(window.confirm("Are you sure you want to leave our site?"))
window.open(url);
}
</script>

<select onchange="movePage(this.value);">
<option value="http://www.google.com/">Google</option>
<option value="internalpage.html">An Internal Page</option>
<option value="http://www.mysite.com/internalpage.html">The same page, with an absolute URI</option>
</select>

texasgal
03-30-2006, 07:31 PM
.alias. - moving the onChange to the options did not work.

Twey - your example will not work for me because all links are actually leaving my site. My example was just that an example. Thank you though.

Any other ideas?

Twey
03-30-2006, 07:44 PM
Ah, very well then:
<script type="text/javascript">
function movePage(url) {
if(url.indexOf("redcrestedwarbler") == -1)
window.open(url);
else if(window.confirm("Are you sure you want to leave our site?"))
window.open(url);
}
</script>

<select onchange="movePage(this.value);">
<option value="http://www.google.com/">Google, without prompt</option>
<option value="http://www.google.com/?redcrestedwarbler">Google, with prompt</option>
<option value="http://www.google.com/search?q=Anything&redcrestedwarbler">Google, with search terms and prompt</option>
</select>

texasgal
03-30-2006, 08:05 PM
Perfect. Thank you. It works great!

dude9er
10-13-2006, 08:01 PM
How do I make this script goto a blank target?


<script language="javascript">
<!--
function pop(question)
{
agree=confirm(question);
if (agree)
{
window.location = "http://www.google.com";
}else
{
alert('Thanks for staying!');
}
}
//-->
</script>

<input type="button" onClick="pop('Are you eure you want to leave?')">

Thanks

DimX
10-13-2006, 08:27 PM
<script language="javascript">
<!--
function pop(question)
{
agree=confirm(question);
if (agree)
{
window.open("http://www.google.com");
}else
{
alert('Thanks for staying!');
}
}
//-->
</script>

<input type="button" onClick="pop('Are you eure you want to leave?')">