Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Prompt Alert

  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Prompt Alert

    When you do an alert confirm you can make if statements to say if they click ok do this and if they click cancel stop the program. Well I am trying to make a prompt that first asks the user to enter in a url and after they do and click enter they url is wraped with img tags.

    I would like to make it so that if the user clicks cancel the program ends b/c right now if the user click okay with no value the result is [img][/img] and if the user clicks cancel the result is [img]null[/img]. Any step in the right direction would be greatly appreciated!

    PHP Code:
    <script language="javascript">
      function 
    imgGenerator(){
          var 
    question=confirm("Would you like to share an image?");
          if (
    question==true){
             var 
    obj=document.getElementById("mngl-board-post-input");
             var 
    imgurl prompt("To Share an image paste the image url here");
             var 
    txt=document.createTextNode("[img]"+imgurl+"[/img]");
             
    obj.appendChild(txt);
          }
          else {}
      }
      
    </script> 

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    I think this covers all your bases...

    Code:
    <script language="javascript">
      function imgGenerator(){
          var question=confirm("Would you like to share an image?");
          if (question==true){
             var obj=document.getElementById("mngl-board-post-input");
             var imgurl = prompt("To Share an image paste the image url here");
    		 if (imgurl==null||imgurl==""){return;}
             var txt=document.createTextNode("[img]"+imgurl+"[/img]");
             obj.appendChild(txt);
          }
          else {
    	  return;
    	  }
      }
      </script>

  • Users who have thanked xelawho for this post:

    ccarrin2 (01-07-2012)

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,459
    Thanks
    0
    Thanked 632 Times in 622 Posts
    What about if someone clicks the "disable JavaScript" button in the confirm instead of the "Ok" or "Cancel" button?

    You would do better to create your own lightbox dialog to collect the information rather than using one that has additional options for debugging purposes in some browsers.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    try this:

    Code:
    var url = prompt('Please enter the URL of your image:', 'http://') || false;
    if (url) {
        url = url.replace(/(?:http:\/\/)+/gi, 'http://') || false; // fix multiple http
    }
    if (url && (url !== 'http://')) {
        /* use the url here */
    } else {
        /* user cancelled */
    }
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #5
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by felgall View Post
    What about if someone clicks the "disable JavaScript" button in the confirm instead of the "Ok" or "Cancel" button?
    What browser has "disable js" as an option for an alert?

    And, anyone who browses with JS turned off may as well go back to paper bulletin boards and cup-with-string communications...
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • Users who have thanked Krupski for this post:

    Philip M (01-08-2012)


  •  

    Posting Permissions

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