I want you to optimize your coding experience #247
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Now, if I need to write code that executes after the page loads, and it needs to execute on every page, I need to write:
The reason for registering both events is that the DOMContentLoaded event will not be executed if the JS is not loaded the first time the page is opened, and pjax:success will not be executed if the JS is loaded the first time the page is opened.
To make matters worse, if the code is embedded in an HTML document (that is, the code only needs to be executed on this page), I need to write:
Because if I don't manually delete the event, the pjax:success event registered on this page will still be executed after I jump to another page, so I have to manually delete the pjax:success event registered on this page. (DOMContentLoaded doesn't matter because it only executes once.)
So I want to be able to add an event that is executed after the current page's DOM has been loaded, no matter how many times it has been loaded.
The second code can be changed to look like this:
So far I have solved this problem by manual encapsulation, I wrote the following in a JS that will be loaded by all pages:
Then use it elsewhere: