using php to check is a javascript file has been loaded? – Education Career Blog

Can I use a script to check if a JS file is loaded?

I have a function, which places a form on a page with javascript controls. I don’t know where the user will use this form, and it might be loaded several times into a page. It strikes me as the best way to handle things if the form itself loads the script, so it doesn’t load if not needed, but this leads me to need to check if the script is already loaded to avoid reloading and adding to page load times and bandwidth use.


No. The page isn’t seen until the PHP script has flushed all its output, by which time it’s too late to do anything. But most browsers are smart enough to only load an external resource once per page anyway.


You should have an asset management system in your PHP to see whats being included into the page.

Ultra simple example (derived from link):

class Page {
    private static $head = array();
    private static $js_assets = array();
    private static $content = '';
    static function add_head($tag) {
        self::$head = $tag;
    static function render_head() {
        foreach (self::$head as $tag) echo $tag;
        foreach (self::$js_assets as $js) echo '<script src="'.$js.'" type="text/javascript"></script>';
    static function render_content() {
        echo self::$content;
    static function read_content($file) {
        require $file;
        self::$content = ob_get_clean();
    static function render_layout($file) {
        require $file;
    static function add_js($js) {
        if (!in_array($js, self::$js_assets)) {
            self::$js_assets = $js;



    <head><?php Page::render_head(); ?></head>
        <div id="header"></div>

        <div id="content"><?php Page::render_content(); ?></div>

        <div id="footer"></div>


<?php Page::add_head('<title>Hello World!</title>'); ?>

Leave a Comment