How to implement JavaScript running in innerHTML in Ajax

Source: Internet
Author: User
Tags add eval implement
Ajax|javascript

After referring to some previous practices, the use of a number of workarounds, and add some additional tags to better implement the innerHTML JavaScript operation, so that the various modules of the page

Good interaction, and data sharing.

Let's look at the following example:

<script>
var title = "Hello";
var time = 0;
var author = "Vickenyang";
var email = "ycg01@software.nju.edu.cn";
</script>
<body>
<div id= "Div_1" >
<span style= "Display:none" >hidden</SPAN>
Hehe
<!--bs-->
<script>
document.write ("a");
</script>
<!--es-->
This is the first page!
<br>
<!--bs-->
<script>
time++;
document.write (Title+time+author);
alert (time);
document.write ("<br>---------------");
</script>
<!--es-->
<input type= "text" name= "test" value= "Test" >
</div>

<script>
function Refreshdiv (DIV)
{
var html = "";
function Changediv ()
{
var oldwrite = document.write;
var oldwriteln = Document.writeln;
document.write = function (str)
{
HTML + str;
}
Document.writeln = function (str)
{
HTML + + str + "\ n";
}
var htmltmp = div.innerhtml;
IE default capitalization, add to support Firefox, in the ointment, will replace all the script value, if only IE application, can log off this trip
Htmltmp = Htmltmp.replace (/script/gi, "script");
alert (HTMLTMP);
var pos_1 = 0;
var pos_2 = 0;
Pos_1 = Htmltmp.indexof ("<SCRIPT>", pos_1);
while (Pos_1!=-1)
{
HTML + htmltmp.substring (pos_2,pos_1);
var Pos_3 = Htmltmp.indexof ("&LT;/SCR" + "ipt>", pos_1);
HTML + htmltmp.substring (pos_1,pos_3+ "<-SCRIPT>". length);
pos_2 = pos_1+ "<SCRIPT>". Length;
Eval (htmltmp.substring (Pos_2,pos_3));
Pos_2 = Htmltmp.indexof ("<!--es-->", Pos_3);
Pos_1 = Htmltmp.indexof ("<SCRIPT>", pos_1+1);
}
HTML + htmltmp.substring (pos_2,htmltmp.length);
document.write = Oldwrite;
Document.writeln = Oldwriteln;
}
Eval ("Changediv ();");
div.innerhtml = html;
}

function Change ()
{
Refreshdiv (document.getElementById (' div_1 '));
}
function Change2 ()
{
document.write ("over");
}
</script>
<input type= "button" value= "Change" >
<input type= "button" value= "Change2" >
</body>

Steps:
1. In the first line of Div plus <span style= "Display:none" >hidden</SPAN> (do not do so, IE will ignore the first JS)
2. In the div before and after each JS plus <!--bs--><!--es--> mark.

Principle: The code will tell you. If you have a better way, please email and discuss it together.

Fill

function Refreshdiv (DIV)
{
var html = "";

If recursive calls are used in a particular application, the HTML here can be defined as a global variable so that the last page is displayed when the recursive refresh is guaranteed. (For example, if you want to have asynchronous load data in Ajax, you can use it when specifying the refresh div in the callback function)

The following examples have specific applications: an AJAX-based, personalized web building prototype

Http://bbs.nju.edu.cn/file/Y/ycg/obj.rar



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.