开发者

External JS not working when called from PHP created HTML

I have a PHP script that creates HTML by calling PHP class that I have created. The class creates all the HTML tags one of which is a tag that loads an external JS file. When I try to access the functions from said file nothing happens. Any Ideas?

index page:

function main(){
    $content = "Heres some text for you";

    $page = new Page($title="MyTitle", $script="external.js", $content=$content)
    echo $page->toString();
}

function __autoload($className){
    require_once $className . '.class.php';
}

class page:

    //class constructor
    function __construct($title='untitled', $script='', $content='Default Page class page'){
    $this->title = $title;
    $this->script = $script;
    $this->stylesheet = $stylesheet;
    $this->content = $content;
    // $this->currentUser = $currentUser;
    }

    // creates tag structure for HTML pages
    function toString(){
    return <<<END
            <!DOCTYPE html>
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="content-type" content="text/html; charset=utf-8" />
            <script type="text/javascript" src="jquery.js"></script>
            // Heres the link to the external JS file
            <script type="text/javascript" src="$this->script"></script>

            <script type="text/javascript">
                 test();
            </script>
            <title>$this->title</title>
            <link type="text/css" rel="stylesheet" href="$this->stylesheet" />
            </head>
            <body>
        $this->content
        <p id='content'>page content</p>
            </body>
            </html>
END;
    }// end toString function

} // end class Page
?>

External JS:

function test(){
 开发者_开发问答   alert("ext. JS test works");
}


You cannot have any spaces before the ending identifier of your heredoc:

        END;

should be:

END;


I would also check to make sure that the path to your external.js file is correct. Are any of the other things working? Like the title or css? You also are not passing $stylesheet into your __construct anywhere which produces an error trying to set $this->stylesheet, maybe the whole script is failing to load because of that?


Don't see anything that stands out....

Are you sure the JS file is accessible in the same directory as your script (may want to apply an absolute or relative path if necessary)?

You might also, since you have jquery (assuming it's loaded), try putting the call to test(); in an "on ready" block, like so:

$(document).ready(function () {
    test();
});

Other than that, I'd use your given browsers debugging tools to see if you can glean anything useful (like the script not even being loaded as a resource).

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜