Skip to content

Instantly share code, notes, and snippets.

@tranquangchau
Created December 3, 2015 16:02
Show Gist options
  • Save tranquangchau/8f4eeff95ebf3d1e7445 to your computer and use it in GitHub Desktop.
Save tranquangchau/8f4eeff95ebf3d1e7445 to your computer and use it in GitHub Desktop.
same ajax in local file html
<head>
<script type="text/javascript" src="test.js"></script>
<a href="javascript:loadjscssfile('myscript.js','js')">Load "myscript.js"</a>
<a href="javascript:loadjscssfile('mystyle.css','css')">Load "mystyle.css"</a>
<div class="demotable" style="background:red;">fsa</div>
<a href="javascript:test()">function test</a>
<div id="content"></div>
</head>
var petname="Spotty"
alert("Pet Name: " + petname)
function test(){
alert("213");
var div = document.createElement('div');
//div.className = 'row';
div.innerHTML='<ul> \
<li>Coffee</li> <li>Tea</li> <li>Milk</li></ul>';
document.getElementById('content').appendChild(div);
}
#demotable {
background:yellow;
}
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)
}
//loadjscssfile("myscript.js", "js") //dynamically load and add this .js file
//loadjscssfile("mystyle.css", "css") ////dynamically load and add this .css file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment