Using Php To Check Is A Javascript File Has Been Loaded?
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
Solution 1:
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.
Solution 2:
You should have an asset management system in your PHP to see whats being included into the page.
Ultra simple example (derived from link):
<?phpclassPage{
privatestatic$head = array();
privatestatic$js_assets = array();
privatestatic$content = '';
staticfunctionadd_head($tag) {
self::$head[] = $tag;
}
staticfunctionrender_head() {
foreach (self::$headas$tag) echo$tag;
foreach (self::$js_assetsas$js) echo'<script src="'.$js.'" type="text/javascript"></script>';
}
staticfunctionrender_content() {
echoself::$content;
}
staticfunctionread_content($file) {
ob_start();
require$file;
self::$content = ob_get_clean();
}
staticfunctionrender_layout($file) {
require$file;
}
staticfunctionadd_js($js) {
if (!in_array($js, self::$js_assets)) {
self::$js_assets[] = $js;
}
}
}
Page::add_js('/javascripts/application.js');
Page::read_content('view.php');
Page::render_layout('layout.php');
?>
layout.php:
<html><head><?php Page::render_head(); ?></head><body><divid="header"></div><divid="content"><?php Page::render_content(); ?></div><divid="footer"></div></body></html>
view.php:
<?php Page::add_head('<title>Hello World!</title>'); ?><h1>Hello</h1><p>World</p>
Post a Comment for "Using Php To Check Is A Javascript File Has Been Loaded?"