This post explains about how to include external javascript files (js files) and styles sheets (css files) using javascript. The following functions to include external javascript files and css(style sheet) files in a one javascript file.
JavaScript Function
function loadjscssfile(filename, filetype){
if (filetype=="js"){ //if filename is a external JavaScript file
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", filename)
}
else if (filetype=="css"){ //if filename is an external CSS file
var fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename)
}
if (typeof fileref!="undefined")
document.getElementsByTagName("head")[0].appendChild(fileref)
}
if (filetype=="js"){ //if filename is a external JavaScript file
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", filename)
}
else if (filetype=="css"){ //if filename is an external CSS file
var fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename)
}
if (typeof fileref!="undefined")
document.getElementsByTagName("head")[0].appendChild(fileref)
}
PHP code to include the javascript and css
To call this functions in one javascript file with
echo "loadjscssfile('".BASEPATH."styles.css', 'css');";
echo "loadjscssfile('".BASEPATH."functions.js', 'js');";
echo "loadjscssfile('".BASEPATH."functions.js', 'js');";
and in a php file starting of the page include below line to treat the php file as a javascript file.
Put this code starting of the file
<?php header("content-type: application/x-javascript"); ?>
Hope that it will be useful.
Pingback : how to include external js,css files with javascript « ANIL KUMAR … – js - dowiedz się więcej!
Pingback : how to include external js,css files with javascript « ANIL KUMAR … – javascript - dowiedz się więcej!
Pingback : how to convert feeds to html using javascript and php - ANIL KUMAR PANIGRAHI 's Blog
“and in a php file starting of the page include below line to treat the php file as a javascript file.”
Do you mean the top of the php get file? Or Bellow the echo base path statement in that file.
There realy is no clarity in what you have said or demonstrated. Sorry, I’m at a complete lose as to how to do this.