1

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");
}
2
  • Sorry in advance about the messy code Commented May 19, 2011 at 18:50
  • What does the generated html look like when you look at the source in your browser? Commented May 19, 2011 at 19:29

3 Answers 3

2

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

        END;

should be:

END;
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, this was my first post and the code transfer was messy. Rest assured that there were no spaces before the ending identifier in the original. Thanks though
0

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?

1 Comment

sorry forgot to delete that. That was left over from deletes i made for the post
0

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!

4 Comments

I have the function call in a ready block but I doesn't seem to make a difference. Firebug isn't showing the external js file (only the jquery one) I think it may have something to do with me setting the source of the script to $this->script not sure why though
Try just changing $this->script to external.js -- if it works, it IS a problem with the $this notation....
Even simpler would be to just view the source and make sure $this->script is translating correctly to external.js If it is, I'd triple-check the location of the external.js file...
AND THE ANSWER IS .... i had one too many closing braces on an if statement in one of the functions in the external JS file. Sorry to have wasted everyones time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.