For smart Primates & ROBOTS (oh and ALIENS ).

Friday, March 13, 2015

Load javascript file dynamic

How to Load javascript file dynamic by button click.

1) First create a javascript file "my_script.js" with below contents:

"my_script.js"
function ss(){
      alert("aa");  
}

2) Create a test.html file with below contents:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function my_dynamic_js(js_file_name) {
var head = document.getElementsByTagName("head")[0];
var sscript = document.createElement("script");
sscript.type = "text/javascript";
sscript.src = js_file_name;
head.appendChild(sscript);
}
</script>
</head>
<body>
<input type="button" value="load" onClick="my_dynamic_js('my_script.js')">
<input type="button" value="show" onClick="ss()">
</body>
</html>

3) now open test.html file and click on "load" button then after click on "show" button
Share:

0 comments:

Post a Comment

Featured Post

Core Web Vitals Assessment showing LCP 10.6s while google pagespeed report showing LCP 3.5s why?

Core Web Vitals Assessment showing LCP 10.6s while google pagespeed report showing LCP 3.5s why? So, you can face this situation while...

Ads Inside Post

Powered by Blogger.

Arsip

Blog Archive