简体   繁体   English

ajax。 如何从请求的 html 文件中调用 javascript 代码

[英]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.另一个.htmlindex.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.

相关问题 带有HTML5,Javascript和AJAX的网络游戏。 这行得通吗? - Networking game with HTML5, Javascript, and AJAX. Would this work? Unity - 与客户端通信 Javascript 和 ajax。如何将数据从统一传回网页? - Unity - communicating with clientside Javascript and ajax. How to pass data back to the webpage from unity? 阿贾克斯 从javascript到asp.cs的字符串 - ajax. string from javascript to asp.cs 如何将字符串数组附加到html div ni ajax? - How to append sting array to html div ni ajax.? 使用AJAX下载文件。 从SD卡。 - Downloading file with AJAX. From a SD card. 如何在AJAX / JavaScript中调用PHP文件? - How to invoke PHP file in AJAX/JavaScript? JavaScript function 从外部呼叫时响应。 但不是从 ajax 的成功部分内部。 如何解决这个问题? - JavaScript function respond when it's call from the outside. But not from the inside of success part of ajax. How to fix this? 如何调用驻留在HTML文件中的一段javascript - how to invoke a piece of javascript that resides in an HTML file 单击时如何包含html文件代码 <li> 使用ajax和javascript? - How to include an html file code on clicking on a <li> using ajax and javascript? 如何将html代码块分配给javascript变量(以调用javascript调用) - how to assign a block of html code to a javascript variable (to invoke a javascript call)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM