0

could you please check if this the right code to do what I pretend? I just want to click on a button, delete the record and return to the same page where I was before. The following sample is working on IE9 and Chrome, but not on IE8.

I´ve got a table with rows, inside a DIV (#page_content). At the end of each row I have a button that allows to delete that record. The button code is the following:

<a class="delete" href="pages/roomwizard/roomwizard_delete.asp?id=<%=Rs("id")%>"><img src="images/delete.png" title="Delete record"/></a>

The delete function is:

$.ajaxSetup({ cache: false }); // it does the job!!!

$('#page_content .delete').click(function (e) {

    e.preventDefault();

    var url_delete = $(this).attr('href');

    $.post(url_delete);

    $('#content').load(url_back); // main div, with #page_content inside

});

As I said, in IE9 and Chrome is working. I don´t know if this is the right way to do it. In IE8, when the button is clicked it still works, but it opens a blank page or stays at the same page (with preventDefault).

ziba
  • 43
  • 8
  • this code seems to be ok, so perhaps there's something else causing an error. Perhaps a console log? http://stackoverflow.com/questions/690251/what-happened-to-console-log-in-ie8 – driedoezoe Oct 17 '13 at 09:54
  • Like I said, it works. But not with the IE8. I think it have something to do with the cache. When I´m using the IE9, the record is deleted and when it returns to the previous page the record is no longer there. With the IE8 it doesn´t happen anything, but when I reopen the browser the data it is update. – ziba Oct 17 '13 at 11:15
  • The code is now working. I´ve updated the code with the solution. – ziba Oct 17 '13 at 13:37

1 Answers1

0

Apart from a missing semi-colon after the "load" method, you could try returning false as well as preventing the default behaviour. Something like this:

$('#page_content .delete').click(function (e) {    
    var url_delete = $(this).attr('href');

    $.post(url_delete, function () {
        $('#content').load(url_back); // main div, with #page_content inside
    });

    e.preventDefault();
    return false;    
});
royse41
  • 2,310
  • 4
  • 22
  • 29
  • The semi-colon and the preventDefault are there, on the original code ;) already try with the the return false, but nothing happens. – ziba Oct 17 '13 at 11:17
  • @ziba see my edit - try reloading the content AFTER the $.post request has returned. – royse41 Oct 17 '13 at 11:21
  • also works in IE9, not in IE8. If I press the F5 to refresh the page the record remains there ... It only desappears when the browser is reopened :/ – ziba Oct 17 '13 at 12:34
  • @ziba sounds like a caching issue which is beyond the scope of this question - but try appending a cache buster to the end of your URLs - $.post(url_delete + '&_t=' + new Date().getTime(), func... – royse41 Oct 17 '13 at 12:44
  • I´ve found the solution! Place this code at the beginning: $.ajaxSetup({ cache: false }); – ziba Oct 17 '13 at 13:35
  • @ziba awesome! that's basically telling jQuery to do what I did manually in my previous comment. glad you got it sorted. don't forget to mark this as the answer if i helped solve your question :) – royse41 Oct 17 '13 at 13:41