Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Confirm onChange

  1. #1
    Join Date
    Nov 2004
    Location
    Texas
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Confirm onChange

    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.

  2. #2
    Join Date
    Mar 2006
    Location
    UK, warwickshire
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    OK think this is what you want.

    put this in your head
    Code:
    <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

    Code:
    <input type="button" onClick="pop('Are you eure you want to leave?')">
    hope it helps

  3. #3
    Join Date
    Nov 2004
    Location
    Texas
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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:
    Code:
    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;
    	}
    }
    HTML Code:
    <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?

  4. #4
    Join Date
    Nov 2004
    Location
    Texas
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Actually that isn't going to work because some don't need to have an Alert message. What do i need to add?

  5. #5
    Join Date
    Mar 2006
    Location
    UK, warwickshire
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    just add the onChange code to each of the options instead of them main select

    Code:
    <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

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <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>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Nov 2004
    Location
    Texas
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    .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?

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Ah, very well then:
    Code:
    <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>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Nov 2004
    Location
    Texas
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Perfect. Thank you. It works great!

  10. #10
    Join Date
    Apr 2006
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    How do I make this script goto a blank target?

    Code:
    <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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •