Jump to content
iwato

Self-Loading Templates (HTML: DOC or Not DOC) - A Question of Strategy

Recommended Posts

i understand the concept:  Write the Javascript with everything well-defined and substitute the existing values with true values via Javascript embedded in PHP.  As the PHP script is executed even before the HTML string reaches the Browser for processing, the true values are already in place before the remaining Javascript is loaded.  Thus, with the exception of placing the script that creates the substitution in the replacement <div> the placement of the script should make no difference.  Still i tried placing the code in different places, but it appears to make no difference. 

Notice that when the data reaches the podcasts.dev.copy.php page from the simulated third-party link clicking on the words Podcast No. 60 realizes the desired effect.  Unfortunately, this effect should already be in place when the page loads.  Triggering the effect via the phrase Podcast No. 60 acts as a pseudo page refresh.

Roddy

Share this post


Link to post
Share on other sites

This is the code that I finally settled for.  Unfortunately, the test alert shows the value of  podcastNo to be false -- with or without the $(document).ready() function.

			<?php 
				if(!empty($_SESSION['podcast_no_item'])) {
					echo "<p id='today'>Podcast No. " . $_SESSION['podcast_no_item'] . "</p>";
					$podcastNo = $_SESSION['podcast_no_item'];
					echo "<script type='text/javascript'>podcastNo = " . $podcastNo . ";</script>";
				}
			?>
			<script>
				$( document ).ready(function() {
					var podcastNo = false;
					alert(podcastNo);
					var podcastInsert = $('#podcast_insert').html();
					if (podcastNo !== false) {
						$('#main').html(podcastInsert);
					}
				});
			</script>	

You can see the above code in podcasts_dev_copy.php.  It appears at line 188 in the <nav> element.

Roddy

Share this post


Link to post
Share on other sites

As the PHP script is executed even before the HTML string reaches the Browser for processing, the true values are already in place before the remaining Javascript is loaded.  Thus, with the exception of placing the script that creates the substitution in the replacement <div> the placement of the script should make no difference.

That is true as long as you're using a page load handler to kick off the Javascript.  If you're not doing that, then placement does matter and the values might not be in place when it gets executed.  You need to only execute the code to run your page after the page finishes loading.

Share this post


Link to post
Share on other sites
Quote

Unfortunately, the test alert shows the value of  podcastNo to be false -- with or without the $(document).ready() function.

That's because you set it to false right before you alert it.  Go back and look at the code I posted, notice how the variable definitions are outside the load handler.  They first get defined with those default values, maybe at some point later down the page PHP will redefine them, and then once the page finishes loading the rest of the Javascript runs, using either the default values or the values that were set by PHP.

  • Thanks 1

Share this post


Link to post
Share on other sites

You gave me the clue I needed.  This appears to do the trick.   

			<?php 
				if(!empty($_SESSION['podcast_no_item'])) {
					echo "<p id='today'>Podcast No. " . $_SESSION['podcast_no_item'] . "</p>";
					$podcastNo = $_SESSION['podcast_no_item'];
					echo "<script type='text/javascript'>podcastNo = " . $podcastNo . ";</script>";
				}
			?>
			<script>
				$( document ).ready(function() {
					var podcastInsert = $('#podcast_insert').html();
					if (typeof podcastNo != "undefined" && podcastNo !== null) {
						$('#main').html(podcastInsert);
					}
				});
			</script>	

Please try the following two links in the order given and tell me if the desired result has been achieved:

http://www.grammarcaptive.com/podcasts_dev_copy.php

http://www.grammarcaptive.com/sender_proxy.php

Roddy

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×