views:

284

answers:

5

How would I go about setting a link to perform certain functions like hide div then go to destined url

something like this

$(document).ready(function(){ $("a").click(function(){ $("#div1").slideUp("slow"); $("#div2").slideUp("slow"); // then go to index.html }); });

This is my setup.

<script type="text/javascript">   $(document).ready(function(){
     $("a").click(function(){
     $("#one").hide("slow");
     $("#two").hide("slow" , function(){
     window.location (index.html);
       });
    });   });    </script>   

<div id="one"> some content </div>

<div id="two"> some content </div>

<a href="#">BUTTON HERE</a>

It only performs the hide function, if I replace the url in the html it perform the url only. I must be doing something Wrong

A: 

What you have is probably about right. Here's what you need to go to a new URL:

top.location.href='someURL';
Joel Meador
A: 

There is nothing in JQuery to redirect from current page to some other page, but JavaScript has it location object, you can use it like:

$(document).ready(function(){ 
    $("a").click(function(){ 
        $("#div1").slideUp("slow"); 
        $("#div2").slideUp("slow"); 
        window.location(url); // or window.location.replace(url);

    }); 
});

You can read more abot redirection in JavaScript here: http://stackoverflow.com/questions/503093/redirect-page-in-jquery

Prashant
actually this will redirect before you can see the animations. window.location call will be executed before the slideUp will finish
Elzo Valugi
+2  A: 

try to put the redirect as callback to be executed after the last slideup:

$(document).ready(function(){ 
    $("a").click(function(){ 
        $("#div1").slideUp("slow"); 
        $("#div2").slideUp("slow", function(){
           window.location(url); // or window.location.replace(url);
        }); 
    }); 
});
Elzo Valugi
This is correct, however I question the need to do an animation before redirecting. Sort of like delaying the inevitable with unnecessary animation.
Ian Elliott
this is exactly what the callback is doing. After the last slideup is finished the redirect is called. check the documentation here http://docs.jquery.com/Effects/slideUp
Elzo Valugi
I have adjusted my code to fit I thought, I edited it above, I wonder what I am doing wrong. yes Ian the animation probably isnt neccesary in most cases, but what I am trying to achieve I would like it.
Anders Kitson
@Elzo, I know what the callback is doing and what's its for, I was just commenting on whether such an effect is actually useful.
Ian Elliott
I don't really question the design issue.You can attach a callback directly to the hide() method without doing any animation. http://docs.jquery.com/Effects/hide.
Elzo Valugi
+2  A: 

"Location" is actually a property of the window object, not a method. This means you need to assign it with =, instead of passing it as a parameter with (). Try this code out:

$("a").click(function(){ 
    url  = $(this).attr("href");
     $("#div1").slideUp("slow"); 
     $("#div2").slideUp("slow", function(){
      window.location=url;
    }); 
    return false;
 });

Also, the default behavior of a link is to go to a URL, and it's going to run that behavior unless you stop it with something like "return false;". While setting the href to "#" won't take a user anywhere, it WILL scroll them to the top of the page, which could be annoying. Better to just return false it.

Doug Avery
I added the url = line as a bonus for users without JS - if they click a "#" link, nothing will happen, which would suck. Instead, you can leave them a normal, working link that goes to http://whatever and just grab that HREF using jQuery for the window.location assignment. Everybody wins!
Doug Avery
return false works most of the time, but if you really want to cancel the link click, a bit of preventDefault() helps too - http://docs.jquery.com/Events/jQuery.Event#event.preventDefault.28.29
Dan F
oh, +1 btw, you're the only one to mention cancelling of the link click, which is likely to be Anders' problem :-)
Dan F
Awesome, thanks for the preventDefault() tip, I hadn't heard of that! Seems like a more widely-applicable answer to problems like this.
Doug Avery
Thanks Works Perfect Now!
Anders Kitson
A: 

Do you really need to set location in javascript? What's wrong with just putting in in the href attribute?

ykaganovich