Home  >  Article  >  Backend Development  >  javascript - When ajax extracts the page for the second time, main.js is not reloaded, or in other words, all js are not reloaded.

javascript - When ajax extracts the page for the second time, main.js is not reloaded, or in other words, all js are not reloaded.

WBOY
WBOYOriginal
2016-08-04 09:20:02894browse

Rendering:
javascript - When ajax extracts the page for the second time, main.js is not reloaded, or in other words, all js are not reloaded.
When using Ajax to create the drawer effect, when ajax extracts the page for the second time, the js is not reloaded. How to solve this problem?

Reply content:

Rendering:
javascript - When ajax extracts the page for the second time, main.js is not reloaded, or in other words, all js are not reloaded.
When using Ajax to create the drawer effect, when ajax extracts the page for the second time, the js is not reloaded. How to solve this problem?

My blog uses similar pjax technology, and I encountered similar problems in the original design.

In fact, the fundamental reason is not that js is not reloaded. In fact, it does not need to be reloaded at all because it has already been loaded.
The fundamental reason is that it will not be re-executed.

You can define a file called bootstrap.js.
Then re-execute the js that need to be reloaded one by one.
ps If you have code written in the public area before, that is, it does not belong to any function, you need to encapsulate it into a function yourself and then execute it here.
For example:
a.js

<code>console.log('test');</code>

This js has actually been loaded and executed. Under normal circumstances, it will not be executed again.
Then you need to modify it to
a.js

<code>var initAJs=function(){
    console.log('test');
}</code>

Then execute
initAjs when pjax returns successfully.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn