UPDATE: Also see this question.
Here is a solution that I have tried and it seems to work correctly, but I'm looking for some feedback on if this is the best method or not... please feel free to comment, and I will make updates as needed. I know that eval()
is dangerous, but I' not sure how dangerous it is in this case.
On your indexfirst page place the following script at the bottom of your page, OUTSIDE of the .content
div, so that it does not get overwritten by push.js
.
<script>
window.addEventListener('push', function(){
var scriptsList = document.querySelectorAll('script.js-custom');
for(var i = 0; i < scriptsList.length; ++i) {
eval(scriptsList[i].innerHTML);
}
});
</script>
Then, on any other page that gets loaded via push.js
, just give the script a class of .js-custom
. This script must be placed INSIDE the .content
div.
<div class="content">
...
<script class="js-custom">
alert('I was executed!');
</script>
</div>
The fist script will find and loop through any <script>
tags with the .js-custom
class and execute it's contents using eval()
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…