Question

I'm trying to build myself a prefetcher for a ModX Revo site I'm building.

I'm now getting a Syntax Error on the <script type="text/javascript"> line

How can I get this to work correctly?

I'm including it in my pages/templates as <script type="text/javascript" src="/prefetcher.php"></script>

And here is the code:

<?php

header('Content-Type: text/javascript');

require_once($_SERVER['DOCUMENT_ROOT'] . '/assets/php/m.inc.php');

//if(!@$_SESSION['AlreadyPrefetched']){
    $sql = "Select `pagetitle`, `longTitle`, `uri` From `modx_site_content` 
            Where `type` = 'document' And `published` = 1 AND 
            (`uri` <> 'portfolio/item' And `uri` <> 'error-page-401' And `uri` <> 'sitemap.xml' And `uri` <> 'error-page-404' And 
            `uri` <> 'error-page-other' And `uri` <> 'index')";
    $qry = $modx->query($sql);
    $rows = $qry->fetchAll(PDO::FETCH_ASSOC);
    $rCt = count($rows);
    if($rCt > 0){
        $_SESSION['AlreadyPrefetched'] = true;
        echo '<script type="text/javascript">
                $(window).on("load", function(){
                    alert("Prefetch Active");';
                for($i = 0; $i < $rCt; ++$i){
                    echo '      $.ajax({ url:"/'. $rows[$i]['uri'] .'", cache:true, dataType:"text", success:function(){}, error:function(){} });';
                }
        echo '  });
            </script>';
    }   

    unset($rows);
//}
?>

Watching FireBug, the request to prefetch.php returns as the correct content-type...

Was it helpful?

Solution

You are outputting <script type="text/javascript"> twice. In your HTML, you have <script type="text/javascript" src="/prefetcher.php"></script> and from inside prefetcher.php, you are again echoing <script type="text/javascript">. You need to take out the script tags from inside the PHP file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top