I try to create modular application, so each page have contains own html and javascript code. I supposed to load all code dynamically like:
var s = document.createElement("script");
s.type = "text/javascript";
s.src='function oncl() { alert("click");}';
$(".selector").append(s);
$( ".selector" ).
append('<input type="button" onclick="ocl();" />
<form action="../test/test_upload4.php"
method="POST" enctype="multipart/form-data" name="getnamefile">
<input type="file" id="uploadfile" name="uploadfile">
<input type="submit" id="Submit" name= "Submit" value="Upload"></form>');
But it doesn't work -Error: ocl is not defined. What can be the reason? If I understand correct in each webpage is an object that contains all javascript functions - so why not possible add or remove function to/from ?
I try to create modular application, so each page have contains own html and javascript code. I supposed to load all code dynamically like:
var s = document.createElement("script");
s.type = "text/javascript";
s.src='function oncl() { alert("click");}';
$(".selector").append(s);
$( ".selector" ).
append('<input type="button" onclick="ocl();" />
<form action="../test/test_upload4.php"
method="POST" enctype="multipart/form-data" name="getnamefile">
<input type="file" id="uploadfile" name="uploadfile">
<input type="submit" id="Submit" name= "Submit" value="Upload"></form>');
But it doesn't work -Error: ocl is not defined. What can be the reason? If I understand correct in each webpage is an object that contains all javascript functions - so why not possible add or remove function to/from ?
Share Improve this question asked Jun 22, 2011 at 13:55 user810430user810430 11.5k16 gold badges40 silver badges44 bronze badges 4- why do you need this kind of hackery? you are using jQuery and onclick in html? – Uku Loskit Commented Jun 22, 2011 at 13:56
- That's a horrible idea. Why are you doing this? – SLaks Commented Jun 22, 2011 at 13:57
- The web application that I am developing is plicate. It like CRM. It contains near 100 pages. So I need to separate to independent pages - to work in paralell. It seems convenient if each module has own javascript and html – user810430 Commented Jun 22, 2011 at 13:57
- You can set the event handler after you append the markup using jquery click or bind method. – ShankarSangoli Commented Jun 22, 2011 at 13:58
4 Answers
Reset to default 2If you'd like to load js file dynamically, you can try as the following:
Here is the HTML page code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript">
function dynamicLoad() {
var s = document.createElement("script");
s.type="text/javascript";
s.language="javascript";
// Here goes your
s.src ="dynamic.js";
document.getElementsByTagName("head")[0].appendChild(s);
s.onreadystatechange = function() {
//window.alert(s.readyState);
}
s.onload= function() {
if (s.readyState == "plete") {
// You must create your DOM element after the js file has been loaded
var btn = document.createElement("input");
btn.type="button";
btn.value="Click Me";
btn.onclick = test;
document.getElementsByTagName("body")[0].appendChild(btn);
}
}
}
</script>
</head>
<body onload="dynamicLoad()">
<!-- a href="javascript:void(0)" onclick="test()">Click Me</a -->
</body>
</html>
Here is the js file:
function test() {
window.alert("HELLO");
}
I tested the code under IE 9. Just FYI
The src
property of the <script>
tag specifies the URL of an external Javascript file, not the text of the script.
If you want to execute code in an arbitrary string, you can call the eval
function.
However, don't.
In your code, you declare a function called oncl and you try to call ocl (with a missing n).
Regards,
Max
in addition to the above answer you have not defined any "ocl" function there is "oncl".
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744802830a4594596.html
评论列表(0条)