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


EmoticonEmoticon

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

No comments:

Post a Comment

Search in This Blog