[SalesForce] Lightning component : call apex function after window.location

In lightning, is it possible to call an Apex function just after a window.location.href ?

For example I have a function that save a pdf from a visualforce page into attachment, and in the lightning component, when I click a button, I want that the pdf (visualforce page with renderAs="pdf") is displayed and just after I want that the function saving the pdf to be called

window.location.href = "my location";
var action = component.get("c.attachPdf");

Best Answer

A browser, once instructed to go to a different page, automatically closes all of its open connections to the server and begins the process of removing all elements from memory. In other words, once you "leave" a page, there's absolutely nothing more that you can do script-wise, including calling to a server, saving page state into local storage, or anything else. Instead, call the Apex function first, and in the callback, perform your redirect.