javascript - Dynamic Load CSS and JS files -
good day everyone.
i have questions regarding dynamic load css , js file.
i know there lot of kind of question around. concerning is:
should use back-end program load css , js?
example:
if($page == "abc.com") { echo "<link href='abc.css' />"; else if($page == "def.com") { echo "<link href='def.css' />"; else { echo "<link href='ghi.css' />"; }
use function load specific css in specific page, declares inside <body> section.
dynamically add css , js file - codeigniter
inside abc.com
add_css(array('metro-admin.css', 'abc.css')); echo put_headers();
and source code this.
ok have 1 solution, instead of declaring css explicitly within php, let php echo <script>
tag in order php not trespass in front-end.
if($page == "abc.com") { echo "<script src='abc.js'></script>"; else if($page == "def.com") { echo "<script src='def.js'></script>"; else { echo "<script src='ghi.js'></script>"; }
ok there's there php side done, we'll crack script execution within individual script files.
--== abc.js ===--
var includefiles = [ 'abc' ]; (var = 0; < includefiles.length; i++) { $('head').append("<link rel='stylesheet' href=' +includefiles[i]+ ".css'>"); }
of course you'd apply round same code each of files, potentially crash in on d.r.y, script execute if it's explicitly echo'd php, not repeat, there's arguments on both sides there.
why use array? portability, if wanted add multiple css files per page, simple add css file name array , loop take on accordingly.
Comments
Post a Comment