0

I'm trying to add a simple popup window to my Django app. How it works is when I press the Yes button, it sends a request to my Django views. However, I want it to automatically close after I press Yes. Unfortunately, the problem with putting it directly in my "click" function is that it closes the window altogether without sending a request to my Django views.

That is why I wanted to add a variable "clicked". After the button is clicked, I want it to change the variable value to "true" so that I can run an if statement ultimately to close the window. For some reason, the popup window automatically closes when I try to open it. My guess is that the if statement is not working properly? Thank you for your help!

$(document).ready(function(){
    var clicked=false;
    var deleteid;
    $('#yes').click(function(){

        deleteid = $(this).attr("data-deleteid");
        $.get ('../../delete_function/', {delete_id:deleteid}, function(data){});
        clicked = true;
    });
    if (clicked = true){
        window.close();
    }
});
13
  • quite a few issues - clicked = true is assignment clicked == true is check - ALso, this needs to be checked in the .get method's callback. Commented Jun 29, 2014 at 23:19
  • thank you! forgot about clicked == true but how do you check in the .get method's callback? Commented Jun 29, 2014 at 23:24
  • put the code in {} of the call back - This check, and clicked=true Commented Jun 29, 2014 at 23:25
  • like this ? $.get ('../../delete_function/', {delete_id:deleteid}, function(data){clicked=true}); Commented Jun 29, 2014 at 23:27
  • Yes - Also, if this is a URL, you do not need the ../.. Commented Jun 29, 2014 at 23:30

1 Answer 1

1

Close the window in the callback function of $.get.

$.get ('../../delete_function/', {delete_id:deleteid}, function(){
    window.close();
});

Your if (clicked == true) is only running when the page is loaded, because it's not inside any event handler. At that point, the button obviously hasn't been clicked yet, so it doesn't do anything.

If you call window.close() directly from the click handler, it will close the window before the AJAX call has a chance to run. When you close a window, all script it was running, including any AJAX operations that were queued, are killed.

Sign up to request clarification or add additional context in comments.

2 Comments

i tried another simple jquery function and it doesnt seem to work. weird how anything in that function won't do any of my jquery requests
Are you sure the AJAX call is succeeding?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.