The solution to the JavaScript contained in the pages loaded with Ajax _ajax related

Source: Internet
Author: User
Tags eval
1, "Use iframe"
Add an iframe to the page you want to load, as follows

Copy Code code as follows:

<iframe style= "Display:none" onload= "Javascript:close_ticket_onload ()" ></iframe>

OnLoad Add the function you want to call, if the loaded page is in the pop-up layer, this function needs to be placed on the parent page that pops up this layer!

2, "Use eval"
If you use innerHTML to populate the data obtained by XMLHTTP. For example: Xxx.innerhtml=xmlhttp data obtained
This is not to execute JS, add the Eval method on the OK: such as: Xxx.innerhtml=eval (back to XMLHTTP obtained data);

The following are supplementary content:
For example: After obtaining data with microsoft.xmlhttp, the data is alert (' Test ')
Xxx.innerhtml= returned data (that is, alert (' Test '))
is not performed because JavaScript is already running when the page is loaded. Adding code to it, he will not execute it.

I didn't find a way at first, but I could only use <iframe> instead. But I think it's not what I wanted. "Do not refresh update data" effect ... But it still can be used.

Later still did not give up, and then to Google search once, incredibly search out a post on the CSDN
Xxx.innerhtml=eval (returned data); such as: alert (' Test ')
It's a pleasure to try and succeed. Thank you so much for this master.
So I searched for the usage of eval.

Use of the Eval function in JavaScript

Introduction: When you need to dynamically create statements, you will find it very unique role, the Internet to see a half-day, to find some scattered content, want to also calculate very comprehensive, all of it gathered up here for later use, but also for everyone to discuss the use of learning.
The eval () function
JavaScript has a number of tips to make programming easier.
One of these is the eval () function, which can execute a string as a JavaScript expression.
As a small 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" in JavaScript is actually executed.
So when you set the value of The_evaled_answer to eval ("2 + 3"), JavaScript will understand and return 2 and 3 and back to The_evaled_answer.
This may seem a bit silly, but it can actually do very interesting things. Like using eval, you can create a function directly from the user's input.


Attached test code:
Copy Code code 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 username");
return false;
}
Infoboard=document.getelementbyid ("Checkinfo");
infoboard.innertext= ' Query ... ';
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 id= "Checkinfo" style= "Display:none" ></span><input type= Button Name=checkuser value= "detects if the user is saving

In the "onclick=" check_user_exists (This.form); >
</form>


The content of form1.asp is
Copy Code code as follows:

Alert (' content is just a test, so write a simple point ')
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.