Javascript: 2 ways to insert new function dynamically
来源:互联网 发布:剑灵人女身材数据 编辑:程序博客网 时间:2024/05/22 07:53
I personally recommend the first way:
function addFunc() ...{
var sFunc = ""
+"function myOnUnLoad()"
+"{"
+"alert('unload...');"
+"};"
+"window.document.body.onunload = myOnUnLoad;";
window.eval(sFunc);
}
var sFunc = ""
+"function myOnUnLoad()"
+"{"
+"alert('unload...');"
+"};"
+"window.document.body.onunload = myOnUnLoad;";
window.eval(sFunc);
}
Second way, by this way you have to write down all your code in a external js file, in this case, let us say myjs.js file:
function addFunc()
...{
var o = window.document.createElement("script");
o.type="text/javascript";
o.src="myjs.js";
//if you use o.innerHTML="..." or o.innerText="..." here, you will get a runtime error.
//that is why you have to use a external js file, and use o.scr="myjs.js" to insert function code
window.document.getElementsByTagName("head")[0].appendChild(o);
}
//-- code in myjs.js file
window.document.body.onunload = function ()
...{
alert("unload...");
}
...{
var o = window.document.createElement("script");
o.type="text/javascript";
o.src="myjs.js";
//if you use o.innerHTML="..." or o.innerText="..." here, you will get a runtime error.
//that is why you have to use a external js file, and use o.scr="myjs.js" to insert function code
window.document.getElementsByTagName("head")[0].appendChild(o);
}
//-- code in myjs.js file
window.document.body.onunload = function ()
...{
alert("unload...");
}
- Javascript: 2 ways to insert new function dynamically
- Howto Dynamically Insert Javascript And CSS
- Add new function to a prototype in javascript
- javascript中的new function
- Dynamically Calling a Function
- JavaScript:to write a function which increments a string, to create a new string
- JavaScript的 new function () {} 写法
- Three ways to define a javascript class
- 3 ways to define a JavaScript class
- ways to creat an object in javascript
- 3 ways to define a JavaScript class
- 3-ways-to-define-a-javascript-class/
- 部分译文 - Summary of Ways to Reference the jQuery Function
- Load javascript file dynamically
- 3 Sure-Fire Ways to Generate New and Great Ideas
- Two ways to create a new thread of execution
- 理解javascript中的function与new Function的区别
- 关于javascript的函数function,new function和new Function的一些理解
- 看《alibaba神话》有感
- 一道面试题
- 变量名前的下划线
- kdb代码分析(二)
- 关于电子商务平台的发展及个人看法
- Javascript: 2 ways to insert new function dynamically
- 欢迎访问 Forcal程序设计 & Lu程序设计
- VS2005 下的串口程序的实现代码
- Java Web 学习路线(2008)
- 那天,她只收了58元
- 无dll无进程木马源代码
- Windows Socket五种I/O模型——代码全攻略
- 客户端减肥记——Ajax带来的困惑
- Golf