Note that there are some explanatory texts on larger screens.

plurals
  1. POdocument.ready is getting called multiple times
    text
    copied!<p>Within my php app I have multiple templates that get combined to form a single page. so say your on the about us page, the template would look like this</p> <pre><code>&lt;?php $this-&gt;render("header.php"); ?&gt; my about us page &lt;?php $this-&gt;render("footer.php"); ?&gt; </code></pre> <p>which is just a fancy way of doing an include(). However, header.php has some javascript that gets ran on document.ready via the jquery framework, and so does the page that requires the header. so you have something like this</p> <pre><code>&lt;?php $this-&gt;render("header.php"); // has its own $(document).ready ?&gt; &lt;script type="text/javascript"&gt; $(document).ready(function(){ console.log("test"); }); &lt;/script&gt; my about us page &lt;?php $this-&gt;render("footer.php"); ?&gt; </code></pre> <p>The bug that I'm experiencing is that document.ready is getting called once for every document.ready on the page. in the above case console shows </p> <p>test</p> <p>test</p> <p>If I comment out the rendering of header.php, the document.ready gets called just once like I want. </p> <p>Besides using a semaphore, is a better way to prevent this? I tried buffering the output, but that doesn't seem to work. </p> <p><strong>Edit: added final output per request</strong></p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function(){ // do some javascript like highlighting // form elements with specific classes }); &lt;/script&gt; display header here &lt;script type="text/javascript"&gt; $(document).ready(function(){ console.log("test"); }); &lt;/script&gt; my about us page display footer </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload