尝试调用从AJAX调用传回的JS代码 [英] Trying to call JS Code that is passed back from AJAX call

查看:76
本文介绍了尝试调用从AJAX调用传回的JS代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有一个可检索一些HTML的javascript函数...

Okay, so I have an javascript function that retrieves some HTML...

function updateQuestions(i){
    var url = 'getQuestions.php?sys=' + i;
    if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
        receiveReq.open("GET", url, true);
        receiveReq.onreadystatechange = handleQuestionsUpdate; 
        receiveReq.send(null);
    }
}

function handleQuestionsUpdate() {
    if (receiveReq.readyState == 4) {
        var a=receiveReq.responseText;
        document.getElementById('questions').innerHTML=a;
        checkSpeakers(); //Error Occurs Here, even though checkSpeakers() is a function in the returned HTML chunk.
    }   
}

此HTML不仅是HTML,而且更具体地说是一种形式和一堆JavaScript.该javascript硬编码到HTML中,未由< script src ="..">

This HTML is not just HTML, but it is more specifically a form and a chunk of javascript. The javascript is hard-coded into the HTML and not referenced by <script src="..">

在调用时无法识别此检索到的JS代码是否正常?如果是这样,如果每次更新div时都需要更改JS,该怎么办?

Is it normal that this retrieved JS code isn't recognized upon call-time? If so, what is my alternative if I need the JS to change every time the div is update?

这是返回到javascript函数的文本.

This is the text being returned to the javascript function.

function checkPillowSpeakers()
{
    var pillowSpeakers = document.getElementById('Q9').value + document.getElementById('Q10').value;
    document.getElementById('PS1').style.display = ((pillowSpeakers > 0)? '' : 'none');
    document.getElementById('PS2').style.display = ((pillowSpeakers > 0)? '' : 'none');
}~ARRAYSEPERATOR~<html>....</html>

通过〜ARRAYSEPERATOR〜标签将JS代码与HTML代码分开.问题是我现在不想执行代码,我只是希望它排队,以便可以在命令中调用它.

The JS Code is seperated from the HTML code by an ~ARRAYSEPERATOR~ tag. The issue is that I don't want to EXECUTE the code at this time, I just want it queued so I can call it on command.

推荐答案

经过大量研究,似乎Eval()存在一些内存问题……所以我实际上遇到了这种解决方案:

After doing a whole lot of research, it seems Eval() has some memory issues... so I actually came across this solution:

//Firstly, create a div called codeHolder

var javascriptCode="function test(){.....}";
var JSONCode=document.createElement("script");
JSONCode.setAttribute("type","text/javascript");
JSONCode.text=javascriptCode;

var cell=document.getElementById("codeHolder");
if ( cell.hasChildNodes() )
    while ( cell.childNodes.length >= 1 )
        cell.removeChild( cell.firstChild );

cell.appendChild(JSONCode);

这篇关于尝试调用从AJAX调用传回的JS代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆