[英]ajax. how to invoke javascript code from requested html file
i have the index.html page.我有index.html页面。
Inside that page i have javascript code that makes a button to evoke ajax request for the other.html to be shown inside a div in index.html . Inside that page i have javascript code that makes a button to evoke ajax request for the other.html to be shown inside a div in index.html .
The other.html have nothing else but a div ( that contains the content ) and some javascript code. other.html只有一个 div(包含内容)和一些 javascript 代码。
The other.html loads normally inside the index.html but the javascript code does not work.另一个.html在index.html中正常加载,但javascript代码不起作用。
Anyone know why is this happening?有谁知道为什么会这样?
Thank you.谢谢你。
( the javascript code is as simple as an alert("hello") message ). (javascript 代码就像警报(“hello”)消息一样简单)。
code:代码:
index.html:索引.html:
<html>
<head>
<script src=main.js></script>
</head>
<body>
<div id="to_change">bla bla bla</div>
<div id="button">click me</div>
</body>
<html>
main.js:主.js:
...
...
function sendRequest()
{
request.open("GET","other.html",true);
request.onreadystatechange = function(){
if(request.readyState == 4 && request.status == 200){
var response = request.responseText;
if(response) {
document.getElementById("to_change").innerHTML = response;
}
}
}
request.send(null);
}
....button.click(...sendRequest...);
...
...
other.html其他.html
<script type=...>alert("hello");</script>
<div>text text text text</div>
javascript is loading but function call is not happening. javascript 正在加载,但 function 调用未发生。
You are not calling the functions in any event.无论如何,您都不会调用这些函数。
<BODY onLoad="alert('hello world!')">
Like this try to call the function.像这样尝试调用 function。 On any event you want
在您想要的任何活动中
Make sure to remove the enclosing ' <doctype...>
', ' <html>
', and ' <body>
' tags from the "other.html" file, in case they are present.确保从“other.html”文件中删除封闭的“
<doctype...>
”、“ <html>
”和“ <body>
”标签,以防它们存在。
You could try running the scripts manually:您可以尝试手动运行脚本:
// ...
if (response) {
var toChange=document.getElementById("to_change"), scripts, i;
toChange.innerHTML = response;
scripts = toChange.getElementsByTagName('script');
for (i=0; i<scripts.length; i++) {
eval(scripts[i].innerHTML);
}
}
Of course, eval is evil , so use at your own risk...当然, eval 是邪恶的,所以使用风险自负......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.