Solution to JavaScript contained in pages loaded with Ajax

Time:2021-11-28

1. [use iframe]
Add an iframe to the page to be loaded, as follows

Copy codeThe code is as follows:
<iframe style=”display:none” onload=”javascript: close_ticket_onload()”></iframe>

Add the function you want to call in onload. If the loaded page is in the pop-up layer, this function needs to be placed on the parent page of the pop-up layer!

2. [use Eval]
If innerHTML is used to fill in the data obtained by xmlhttp. For example: XXX. InnerHTML = data obtained by xmlhttp
In this way, JS will not be executed, and it is OK to add the eval method: for example, XXX. InnerHTML = Eval (return the data obtained by xmlhttp);

The following are supplementary contents:
For example: after obtaining data with microsoft.xmlhttp, the data is alert (‘test ‘)
XXX. InnerHTML = returned data (i.e. alert (‘test ‘))
It won’t be executed because JavaScript has been executed when the page is loaded. If you add code to it, it won’t be executed again

At first, I didn’t find a way, so I had to use < iframe > instead. But in fact, I don’t think it’s the effect of “don’t refresh and update data” I originally wanted… But it still works

Later, I still didn’t give up. I searched Google again and found a post on CSDN
XXX. InnerHTML = Eval (returned data); For example: Alert (‘test ‘)
I’m so happy that I succeeded in the first attempt.. thank you so much for your expert
So I searched for the usage of eval

Use of Eval function in JavaScript 

Introduction: when you need to dynamically create sentences, you will find its very unique role. After reading the Internet for a long time, you will find some scattered content. I think it is very comprehensive. Collect it all and put it here for later use, as well as for discussion, learning and use.
[eval() function]
JavaScript has many tips to make programming easier.
One of them is the eval () function, which can execute a string as a JavaScript expression.
For example:
var the_unevaled_answer = “2 + 3”;
var the_evaled_answer = eval(“2 + 3”);
alert(“the un-evaled answer is ” + the_unevaled_answer + ” and the evaled answer is ” + the_evaled_answer);
If you run this Eval program, you will see that the string “2 + 3” is actually executed in JavaScript.
So when you put the_ evaled_ When the value of answer is set to eval (“2 + 3”), JavaScript will understand and return the sum of 2 and 3 to the_ evaled_ answer。
This seems a little silly, but it can actually do very interesting things. For example, with Eval, you can directly create functions based on user input.

Attach test code:

Copy codeThe code is as follows:
<script language=javascript>
function check_user_exists(form){
u_name=form.u_name.value;
if (u_name==null||u_name==”){
Alert (“please enter your user name”);
return false;
}
infoBoard=document.getElementById(“checkInfo”);
Infoboard. InnerText = ‘querying…’;
myurl=”http://localhost/ajax/form1.asp”
retCode=openUrl(myurl);
infoBoard.innerHTML=eval(retCode);

return;
}

function openUrl(url){
var objxml=new ActiveXObject(“Microsoft.XMLHttp”)
objxml.open(“GET”,url,false);
objxml.send();
retInfo=objxml.responseText;
if (objxml.status==”200″){
return retInfo;
}
else{
return “-2”;
}
}
</script>

<form name=form1 action=”XXXX.asp” method=”post”>
<input type=text name=u_ Name > < span style = “display: None” > < / span > < input type = button name = checkuser value = “check whether the user saves

In “onclick =” check “_ user_ exists(this.form); “>
</form>

The content of form1.asp is

Copy codeThe code is as follows:
Alert (‘the content is only a test, so write it simple ‘)