onclick: next method may only be executed if first is true

Discussion in 'JavaScript' started by appeltaart, Mar 22, 2007.

  1. #1
    See below,

    The second method may only be executed if the result of the first method is true. Now second method is always being executed. Does someone has a flexible solution?


    <html>

    <script type='text/javascript'>
    function first()
    {
    var agree=confirm("Go to second method?");
    if (agree)
    return true ;
    else
    return false ;
    }

    function second()
    {
    var agree=confirm("Are you sure?");
    if (agree)
    return true ;
    else
    return false ;
    }
    </script>

    <a href="#" onclick="first();second();return false;">click</a>
    </html>
     
    appeltaart, Mar 22, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Try this.
    
    <script type='text/javascript'>
    function first()
    {
        if (confirm("Go to second method?"))
        {
             second();
        }
    }
    
    function second()
    {
        return confirm("Are you sure?");
    }
    </script>
    
    Code (javascript):
    
    <a href="#" onclick="first(); return false;">click</a>
    
    HTML:
     
    nico_swd, Mar 22, 2007 IP
  3. Adi

    Adi Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    As I remember both functions should and will be executed but the return value of the onClick() will cause the browser to navigate to the link in the href or stay in the same page.
     
    Adi, Mar 22, 2007 IP
  4. appeltaart

    appeltaart Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Actually I don't want to have a direct reference to the second method in the first, mayby the pseudo code below is explaining a bit more:

    <html>

    <script type='text/javascript'>
    function first(method)
    {
    var agree=confirm("Are you sure?");
    if (agree)
    method;
    }

    function second()
    {
    var agree=confirm("Are you sure2?");
    if (agree)
    return true ;
    else
    return false ;
    }

    function thirt()
    {
    var agree=confirm("Are you sure2?");
    if (agree)
    return true ;
    else
    return false ;
    }
    </script>

    <a href="#" onclick="first(second());return false;">KLIK</a>

    <a href="#" onclick="first(thirt());return false;">KLIK</a>

    </html>
     
    appeltaart, Mar 22, 2007 IP
  5. The Reckoning

    The Reckoning Peon

    Messages:
    862
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    function check () {
    	if (one())
    		if (two())
    			three();
    }
    function one () {
    	return confirm('Are you sure?');
    }
    function two () {
    	return confirm('Are you sure, seriously?');
    }
    function three () {
    	alert('Alright, I understand you are sure!');
    }
    
    Code (javascript):
    
    <a href="#" onclick="check(); return false;">Click me!</a>
    
    Code (html4strict):
    Works for me, test here: www.zelune.com/test/
     
    The Reckoning, Mar 22, 2007 IP
  6. appeltaart

    appeltaart Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks, almost but I don't want to have that logica in my javascript but within the onclick..
     
    appeltaart, Mar 23, 2007 IP
  7. The Reckoning

    The Reckoning Peon

    Messages:
    862
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #7
    
    <a href="#" onclick="if (confirm('Are you sure?')) {if (confirm('Are you sure, seriously?')) {alert('Alright, I understand you are sure!')}}; return false;">Click me!</a>
    
    Code (html4strict):
    Enjoy.
     
    The Reckoning, Mar 23, 2007 IP
  8. appeltaart

    appeltaart Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    i solved it:
    <a href="#" onclick="first() && second();return false;">click</a>
     
    appeltaart, Mar 26, 2007 IP
  9. Adi

    Adi Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Nice ... I forgot that JS uses lazy evaluation - and this works with both with IE and FF
     
    Adi, Mar 26, 2007 IP