Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    JavaScript Syntax Error?

    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:

    Code:
    <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>

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,877
    Thanks
    6
    Thanked 1,035 Times in 1,008 Posts
    Quote Originally Posted by Firefox error console
    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?

  • Users who have thanked VIPStephan for this post:

    thebigkrumm (07-16-2009)

  • #3
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Bingo.

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

    ** For the record:

    Fixed by replacing:

    Code:
    $description = $item->get_description();
    $desclength = strlen($description);
    if ($desclength < 200) {
    echo $description;
    }
    with the following:

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •