ANSWERED! See bottom of original question for the working answer.
I'm trying to find out how I can run a piece of JavaScript code the moment a list item gets deleted. So it should be some sort of script that runs on the page, and triggers when a user deletes an item from a list.
For example, this dummy code should somewhat tell you what I mean:
function doThisOnDelete() {
if (action == delete) {
alert('You just deleted a list item.');
}
else {
do nothing
}
}
So on the line with "(action == delete)", it should be something that detect if a list item gets deleted.
Is there a way to check when a method is called (like the one that deletes list items)? And how can I find out what the name of that method would be?
My goal is to return to the previous page when an item gets deleted, or to prevent the page from going to the list and leave the page with the custom list view.
Any help is greatly appreciated!
Alternatively, I'm also open to the idea of using custom JavaScript code to delete a list item (based on the paragraph titled "Delete a file in a document library" from the MSDN site).
If I can manage to use a custom delete script in JavaScript, then I can perhaps prevent the user from automatically being redirected. If this is possible, then any suggestions and/or tips regarding this are welcome as well!
ANSWER
Inspired by Ecanem's answer, I wrote the following code. Against my expectations, it ended up being rather simple and short.
var ref = document.referrer; // Stores the URL of the previous page.
var url = window.location.pathname; // Stores the URL of the current page.
// The following code will run if the user edits a list item or properties of a document.
if (url.indexOf('EditForm.aspx') > -1) {
sessionStorage.setItem("page", ref);
}
if (url.indexOf('AllItems.aspx') > -1 && ref.indexOf('EditForm.aspx') > -1) {
window.location = sessionStorage.getItem("page");
}
It works like this: first we see if the current URL is "EditForm.aspx". If it is not, do nothing. If it is, then we save the URL of the previous page in the session.
If the URL of the current page is "AllItems.aspx" and the previous page was "EditForm.aspx", then this means that an item or document was deleted from the list or library by the user.
Since we want to prevent users from seeing the "AllItems.aspx" page and let them stay on the page with the list/library view, we will redirect them to the page we stored in the session.
The redirection is smooth and almost unnoticeable; the URL for "AllItems.aspx" may be present in the browser address bar for a short moment, but most users won't focus on the address bar and won't even know they have been redirected back to the original page.
There is, however, a very small issue.
When you log something to the console while using this code, you may get an error saying "Uncaught ReferenceError: Type is not defined" and then you won't be able to redirect back to the proper page (in my case, it ended up redirecting to a completely different page). But as long as you don't include "console.log();" in the code, you'll be just fine!
I also added the answer to my blog, you can find the post here.