-1

I have two buttons: remove and update. Those buttons when they are clicked load to http://homepage/?removed_item=1 or http://homepage/cart.

So what I want is just to redirect to homepage. Is that possible with jquery ?..

their classes are: .remove and .update

EDIT: after the button has been clicked and has done it's process, it will redirect

jp_dev
  • 1
  • 1
  • 3
  • 1
    Do you mean after the button has been clicked and has done it's process, it will redirect. Or do you just want them to go straight to the homepage regardless? – Stewartside Sep 07 '15 at 15:01
  • http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-using-jquery?rq=1 – Madalina Taina Sep 07 '15 at 15:28
  • @Stewartside yes, thank you.. after the button has been clicked and has done it's process, it will redirect, i edit my original post.. – jp_dev Sep 08 '15 at 07:30
  • Then that is done on the actual pages themselves and not on the click events. You'll need to provide the code for the processes that it runs on the two pages – Stewartside Sep 08 '15 at 07:39

2 Answers2

0

You can do something like that:

$(".remove").click(function() {
  window.location = "http://homepage/";
});

$(".update").click(function() {
  window.location = "http://homepage/";
});
Climbatize
  • 1,103
  • 19
  • 34
0

UPDATED

event.preventDefault() : If this method is called, the default action of the event will not be triggered.

You should use JQuery method event.preventDefault() to prevent default href and then redirect to homepage.

HTML :

<a href='http://homepage/cart' class='update'>Update</a>
<a href='http://homepage/?removed_item=1' class='remove'>Remove</a>

JS :

$(".update, .remove").click(function(e) {
    e.preventDefault();
    window.location.replace("http://homepage/");
});

If you want to redirect after finishing process, you have to use JQuery function $.get().

JS :

$(".update, .remove").click(function(e) {
    e.preventDefault();

    $.get($(this).attr('href'),function(result){
        window.location.replace("http://homepage/");        
    })
});

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
  • event.preventDefault() is not a jQuery method – Marc Sep 07 '15 at 15:26
  • [jQuery event.preventDefault() Method](http://www.w3schools.com/jquery/event_preventdefault.asp). – Zakaria Acharki Sep 07 '15 at 15:29
  • https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault To be clear: it's also an method of the "jquery event arg"! – Marc Sep 07 '15 at 15:32
  • I tried that but it doesn't do the remove and update first and the redirect. Is there another way ? I edit my original post too, to explain more! – jp_dev Sep 08 '15 at 07:33
  • To summarize the event discussion in the comments. "jQuery's event system normalizes the event object according to W3C standards. The event object is guaranteed to be passed to the event handler. Most properties from the original event are copied over and normalized to the new event object."[jQuery Event Object](http://api.jquery.com/category/events/event-object/) – Marc Sep 14 '15 at 13:46