...

View Full Version : JavaScript Syntax Error?



thebigkrumm
07-16-2009, 04:53 PM
Sigh, another day, another JavaScript problem.

EXISTING SCENARIO: I have a slow-loading DIV that delays loading my page.

DESIRED BEHAVIOR: I'd like to load a fast-loading place-holder DIV (id "stats") that will allow the rest of my page to load very quickly. At the end of the body I'd like to load the DIV (which pulls from WordPress) and, when it's finished loading, substitute the content into the placeholder DIV using document.getElementById("stats").innerHTML. It sounds fairly easy ...

PROBLEM: My JavaScript doesn't execute. I think I've isolated the issue to the content2 variable (isolate is a relative term), and I suspect it's a quick syntax or apostrophe error - but I've spent hours trying to figure out where I've gone wrong. The script does execute if I place it directly in the DIV.

See the entire page at www.sarasotapropertygroup.com/index.php.


Here's the relevant javascript:



<script type="text/javascript">
var stats = document.getElementById("stats");
var content = "<div><?php echo "test content"; echo "<br>Still Testing" ?></div>";
var content2 = "<p>
<?php $feed_url = 'http://www.sarasotapropertygroup.com/wordpress/wp-rss.php';
$max_items = 5;

//Load SimplePie
include 'simplepie.inc';

//Fetch the RSS feed
$feed = new SimplePie($feed_url);

//Check for errors
if ($feed->error())
echo 'Error : ',$feed->error();

//Output up to $max_items posts
foreach ($feed->get_items(0, $max_items) as $item):
?>
</p>
<div class='item'>
<h3 class='title'>
<?php $title = $item->get_title(); ?>
<div class='smallcaps'>
<a href='pages/marketstats.html'><?php echo $title;?></a>
</div>
</h3>
<?php
// Pulls from the wordpress RSS feed, counts the number of characters; if total chars is under $desclength, returns the entire string, else ...

$description = $item->get_description();
$desclength = strlen($description);
if ($desclength < 200) {
echo $description;
}
else { // ... it scans the string and ends it when the string hits a space, thereby always ending with an entire word.
$descAmount = '200';
$descEnd = $description[$descAmount];
$charTest = ord($descEnd);
if ($charTest == 32){ // 32 = space bar
echo substr($description, 0, $descAmount);
echo ' ...';
}
else {
while ($charTest != 32) {
$descAmount++;
$descEnd = $description[$descAmount];
$charTest = ord($descEnd);
if (ord($descEnd) == 32){
echo substr($description, 0, $descAmount);
echo ' ...';
} } } }
?>
<p> </p>
</div>
<?php
endforeach;
?>";

stats.innerHTML = content2;
</script>

VIPStephan
07-16-2009, 05:31 PM
Error: unterminated string literal
Source: http://www.sarasotapropertygroup.com/index.php
Line: 397, Column: 15
Source code:
var content2 = "<p></p><div class='item'><h3 class='title'><div class='smallcaps'><a href='pages/marketstats.html'>Colonial Oaks Home Sells For $205,000!</a></div></h3>This month we closed our listing at 5539 Wilde Oak Way for $205,000 - which

Could it be?

thebigkrumm
07-16-2009, 09:18 PM
Bingo.

The carriage returns messed me up ... I knew it was simple!

** For the record:

Fixed by replacing:


$description = $item->get_description();
$desclength = strlen($description);
if ($desclength < 200) {
echo $description;
}


with the following:


$description = $item->get_description();
$desclength = strlen($description);
$description = str_replace("\n", " ", $description);
if ($desclength < 200) {
echo $description;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum