开发者

Javascript wont run when include tags are echoed to <head> - PHP, JS, HTML

Hey, I just made a simple function to load JavaScript files from a template view, like this:

public function includeJavascript($file){   
        if(file_exists('path/to/js/' . $file)){
            $this->set('script', '<script type="text/javascript" src="/path/to/js/' . $file . '"></script>');
        }
    }

The string is passed via $script and gets printed in the main layout. This means that when I render the page I actually get: <script type="text/javascript" src="/path/to/js/myjavascript.js'"></script> in the HTML, but the JavaScript code wont run. Why 开发者_运维问答is this?

Sincerely, Why


Temporarily add this line to the top of myjavascript.js as a sanity check..

alert('found myjavascript.js OK!');


Try this:

public function includeJavascript($file){   
        if(file_exists('path/to/js/' . $file)){
            $this->set('script', '<script type="text/javascript" src="/path/to/js/' . $file . '"></script>');
        }
    }

Addded an apostrophe prior to path/to/js/ in the file_exists function.

If you look at the code you posted in the HTML:

<script type="text/javascript" src="/path/to/js/myjavascript.js'"></script>

Note the extra apostrophe after myjavascript.js? That's why it won't work.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜