I might be doing something stupid. But if I have a normal link like:
<div id="changeMe"></div>
<a href="/Not/Intercepted" id="interceptMe">A link</a>
and I attach a jQuery click event to the link like so:
$('#interceptMe').click(function() {
$('#changeMe').text('Changed');
return false;
});
Everything works peachy. The page does not get redirected to /Not/Intercepted, which is what I would think would be correct.
Now...
I introduct a ajax call like $.get to my click event and now my page will be incorrectly redirected to the page, which essentially overwrites the ajax call.
$('#interceptMe').click(function() {
$.get('/Ajax/Call', goesIn, function(comesOut) {
$('#changeMe').html(comesOut);
}, "html");
return false;
});
Is there a way to make jQuery or javascript still intercept the link click so it does not go to the href page? I want to keep the href for those users that aren't enabling javascript. TIA!
jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!
send = function(data) { var self = this; var oldOnReadyStateChange; var url = this. _url; function onReadyStateChange() { if(self. readyState == 4 /* complete */) { /* This is where you can put code that you want to execute post-complete*/ /* URL is kept in this.
ajax appears to always follow redirects. How can I prevent this, and see the redirect without following it? There are various questions with titles like "jquery ajax redirect" but they all appear to involve accomplishing some other goal, rather than just directly checking the status that a server gives.
instead of return false, use ....
$("#interceptMe").click(function(event){
event.preventDefault();
// Ajax here
return false; //for good measure
});
http://docs.jquery.com/Events/jQuery.Event#event.preventDefault.28.29
I've had many issues with IE especially not listening to return false. Apparently so have others http://coffeeandpaste.blogspot.com/2009/02/javascript-onclick-return-false-does.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With