Results 1 to 8 of 8

Thread: Very simply maybe

  1. #1

    Thread Starter
    Hyperactive Member AvisSoft's Avatar
    Join Date
    Sep 2002
    Location
    Chandigarh
    Posts
    459

    Angry Very simply maybe

    Hi!

    I am unable to detemine how to do the following:

    I have a basic html <ahref link:
    Code:
    <a href="test.php?id=1">Test</a>
    Now when user clicks on the above links it must be confired weather he wants to go at it or not...i mean just a yes, no box, if user presses yes then he's taken to that link else just stay in there.

    How can i achieve thus using javascript ?

    Thanks!
    Tapan Bhanot,
    CEO, Avis Software.
    Website: www.avissoftware.com

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986

    Re: Very simply maybe

    Something to the extend of:
    Code:
    function takeme(url){
    if (confirm("Are you sure?")) { 
      location.href=url;
    } else 
    {
      //clicked cancel - do nothing
    }
    }
    the something like
    Code:
    <a href="javascript:takeme('http://google.com');">click</a>
    On a sidenote.. I think if the user clicks on a link he's pretty sure he wants to go there so this script seems abit useless to me, sorry.
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Very simply maybe

    No! Bad Jop!

    This solution is inaccessible. It's better to do:
    <a href="target.html" onclick="return confirm("Really?")">Hit Me!</a>

    This way, users without JavaScript can use the link. (They don't get the confirmation though.)


    Of course, it would be even better not to put the script inline, but attach it from an external JS file.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Very simply maybe

    A better way would be to use the onclick event. Its also easier and means the link will still work even if Javascript is disabled:
    Code:
    <a onclick="return confirm('Are you sure?')" href="test.php?id=1">Test</a>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Hyperactive Member AvisSoft's Avatar
    Join Date
    Sep 2002
    Location
    Chandigarh
    Posts
    459

    Re: Very simply maybe

    Hi!

    Thanks for the help. Can i have yes/no buttons as Proceed/Cancel ?

    Thanks!
    Tapan Bhanot,
    CEO, Avis Software.
    Website: www.avissoftware.com

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Very simply maybe

    No. Some browsers will use Yes/No, other OK/Cancel, but you have no control over it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986

    Re: Very simply maybe

    Oi oi thanks for correcting me CornedBee I should've known better!

    To make up for my mistake, the unobtrusive version:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <head>
    	<title>Link confirmation</title>
    	<script type="text/javascript">
    	function confirmLink(url){
    		if(confirm("Are you sure?"))
    			location.href=this.getAttribute("href");
    		else
    			return false;
    	}
    	
    	var addOnclickHandlers = function(){
    		var links = document.getElementsByTagName("a");
    		for(var x=0;x<links.length;x++){
    			if(links[x].className == "confirm")
    				links[x].onclick = confirmLink;
    		}
    	}
    
    	window.onload = addOnclickHandlers;
    	</script>
    </head>
    
    <body>
    <a href="http://google.com" class="confirm">google</a>
    </body>
    </html>
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  8. #8

    Thread Starter
    Hyperactive Member AvisSoft's Avatar
    Join Date
    Sep 2002
    Location
    Chandigarh
    Posts
    459

    Re: Very simply maybe

    Hi!

    Thanks for all the suggestions and comments. I used visualAd's code which is working cool!

    Thanks!
    Tapan Bhanot,
    CEO, Avis Software.
    Website: www.avissoftware.com

Posting Permissions

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



Click Here to Expand Forum to Full Width