Skip to content Skip to sidebar Skip to footer

How To Click A Link That Will Execute But Not Redirect.

I have a few products in a mySQL database whose links are being retrieved in my script with $producturl. These url's actually add the product to the cart (http://www.example.com/ca

Solution 1:

Your HTML anchor link:

<a id='link1' href="http://www.google.com">Link</a>

The jQuery JS required (don't forget to put this inside a DOM ready function):

// Act on clicks to a elements
$("#link1").on('click', function(e) {
    // prevent the default action, in this case the following of a link
    e.preventDefault();
    // capture the href attribute of the a elementvar url = $(this).attr('href');
    // perform a get request using ajax to the captured href value
    $.get(url, function() {
        // success
    });
});

This demonstrates all the principles required to implement your function.

The page in question and the URL POSTed to must be on the same subdomain, or cross origin resource sharing must be appropriately enabled on the server otherwise the request will fail due to cross domain restrictions.

Solution 2:

Try:

'<ahref="#"id="shop">Add Cart NoRedirect</a>'

or

'<ahref="javascript: return false;"id="shop">Add Cart NoRedirect</a>'

Solution 3:

You can use an image, a button, a div or a text decorated as link instead of a link.

Solution 4:

'<ahref="javascript: void(0);"id="shop">Add Cart NoRedirect</a>'

Post a Comment for "How To Click A Link That Will Execute But Not Redirect."