Solutions to the Javascript contained in pages loaded using Ajax

Source: Internet
Author: User

1. [Use IFRAME]
Add an IFRAME to the page to be loaded, as shown below:

CopyCode The 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 should 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 the eval method will be OK, for example: XXX. innerhtml = eval (returning the data obtained by XMLHTTP );

The following is a supplement:
For example, when Microsoft. XMLHTTP is used to obtain data, the data is alert ('test ')
Xxx. innerhtml = returned data (that is, alert ('test '))
It will not be executed, because JavaScript has been executed during page loading. It will not be executed if code is added to it.

I didn't find a method at the beginning. I can only use <IFRAME> instead. But I don't think it's the effect I originally wanted to "Don't refresh and update data"... but it still works.

After that, I did not give up. I went to Google to search for a post on csdn.
Xxx. innerhtml = eval (returned data); for example: Alert ('test ')
It's a pleasure to have succeeded. Thank you very much.
So I searched for eval usage.

Use of eval functions in Javascript

Introduction: When you need to dynamically create a statement, you will find it very unique. After reading it online for a long time, you can find some scattered content and think about it very comprehensively, all of them are collected here for future use and for discussion 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 evalProgram, You will see that the string "2 + 3" in Javascript is actually executed.
So when you set the_evaled_answer to eval ("2 + 3"), JavaScript will understand and return the sum of 2 and 3 to the_evaled_answer.
This seems a bit silly, but it can actually make interesting things. For example, you can use eval to directly create a function based on user input.

Attached 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 ("Enter the 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 id = "checkinfo" style = "display: none "> </span> <input type = button name = checkuser value =" check whether a user is saved

In "onclick =" check_user_exists (this. form); ">
</Form>

The content of form1.asp isCopy codeThe Code is as follows: Alert ('content is only a test, so it is easy to write ')

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.