Use ajax technology to dynamically call stock information without refreshing

Source: Internet
Author: User

The Code is as follows:
<Html>
<Head>
<Title> ajax test </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Script type = "text/javascript" src = "jquery. js"> </script>
<Script type = "text/javascript">...
Function ajaxRequest ()...{
$. Ajax (...{
Url: 'http: // response ',
Type: 'get ',
DataType: 'html ',
Timeout: 2000,
Success: function (response )...{
Var stocks = response. split (';');
For (var I = 0; I <stocks. length-1; I ++ )...{
Var content = stocks [I];
Var temp1 = content. split ('=') [0];
Var temp2 = content. split ('=') [1];
Var code = temp1.substr (temp1.length-6, 6 );
Var temp3 = temp2.replace ('"','');
Var name = temp3.split (',') [0];
Var tday_f = temp3.split (',') [1];
Var yest_f = temp3.split (',') [2];
Var curr_f = temp3.split (',') [3];
Var temp_f = curr_f-yest_f;
Certificate ('{a'{ I }.html (code );
Certificate ('{ B '{ I }.html (name );
If (curr_f> yest_f )...{
Certificate ('{c'{ I }.html ("<font color = 'red'>" + curr_f + "</font> ");
} Else if (curr_f <yest_f )...{
Certificate ('{c'{ I }.html ("<font color = 'green'>" + curr_f + "</font> ");
} Else ...{
Certificate ('{c'{ I }.html (curr_f );
}
Certificate ('mongod'{ I }.html (tday_f );
Certificate ('{e'{ I }.html (yest_f );
If (temp_f> 0 )...{
('{F'{ I }.html ("<font color = 'red'>" + temp_f.toFixed (2) + "</font> ");
Certificate ('{g'{ I }.html ("<font color = 'red'>" + (temp_f/yest_f) * 100). toFixed (2) + "</font> % ");
} Else if (temp_f <0 )...{
Certificate ('{f'{ I }.html ("<font color = 'green'>" + temp_f.toFixed (2) + "</font> ");
Certificate ('{g'{ I }.html ("<font color = 'green'>" + (temp_f/yest_f) * 100). toFixed (2) + "</font> % ");
} Else ...{
('{F'{ I }.html (temp_f.toFixed (2 ));
Certificate ('{g'{ I }.html (temp_f/yest_f) * 100). toFixed (2) + "% ");
}
('{H'{ I }.html (temp3.split (',') [4]);
('{ I '{ I }.html (temp3.split (',') [5]);
}
}
});
}
Function pageInit ()...{
Windows. setInterval ("ajaxRequest ()", 3000 );
}
</Script>
<Style>...
. Tr_cls {...}{
Height: 30px;
Font-size: 16px;
Font-family: "Times New Roman", Times, serif;
Background-color: # FFFFCC
}
</Style>
</Head>
<Body onLoad = "pageInit ();">
<Form>
<Table width = "800" border = "1" align = "center" cellpadding = "0" cellspacing = "0" bordercolor = "#000000">
<Tr bgcolor = "# 3399FF" height = "30px">
<Th scope = "col"> stock code </th>
<Th scope = "col"> stock name </th>
<Th scope = "col"> current price </th>
<Th scope = "col"> opened today </th>
<Th scope = "col"> close yesterday </th>
<Th scope = "col"> current price difference </th>
<Th scope = "col"> increase/decrease </th>
<Th scope = "col"> maximum price </th>
<Th scope = "col"> lowest price </th>
</Tr>
<Tr class = "tr_cls">
<Td align = "center"> <span id = "a0"> </span> </td>
<Td align = "center"> <span id = "b0"> </span> </td>
<Td align = "center"> <span id = "c0"> </span> </td>
<Td align = "center"> <span id = "d0"> </span> </td>
<Td align = "center"> <span id = "e0"> </span> </td>
<Td align = "center"> <span id = "f0"> </span> </td>
<Td align = "center"> <span id = "g0"> </span> </td>
<Td align = "center"> <span id = "h0"> </span> </td>
<Td align = "center"> <span id = "i0"> </span> </td>
</Tr>
<Tr class = "tr_cls">
<Td align = "center"> <span id = "a1"> </span> </td>
<Td align = "center"> <span id = "b1"> </span> </td>
<Td align = "center"> <span id = "c1"> </span> </td>
<Td align = "center"> <span id = "d1"> </span> </td>
<Td align = "center"> <span id = "e1"> </span> </td>
<Td align = "center"> <span id = "f1"> </span> </td>
<Td align = "center"> <span id = "g1"> </span> </td>
<Td align = "center"> <span id = "h1"> </span> </td>
<Td align = "center"> <span id = "i1"> </span> </td>
</Tr>
<Tr class = "tr_cls">
<Td align = "center"> <span id = "a2"> </span> </td>
<Td align = "center"> <span id = "b2"> </span> </td>
<Td align = "center"> <span id = "c2"> </span> </td>
<Td align = "center"> <span id = "d2"> </span> </td>
<Td align = "center"> <span id = "e2"> </span> </td>
<Td align = "center"> <span id = "f2"> </span> </td>
<Td align = "center"> <span id = "g2"> </span> </td>
<Td align = "center"> <span id = "h2"> </span> </td>
<Td align = "center"> <span id = "i2"> </span> </td>
</Tr>
<Tr class = "tr_cls">
<Td align = "center"> <span id = "a3"> </span> </td>
<Td align = "center"> <span id = "b3"> </span> </td>
<Td align = "center"> <span id = "c3"> </span> </td>
<Td align = "center"> <span id = "d3"> </span> </td>
<Td align = "center"> <span id = "e3"> </span> </td>
<Td align = "center"> <span id = "f3"> </span> </td>
<Td align = "center"> <span id = "g3"> </span> </td>
<Td align = "center"> <span id = "h3"> </span> </td>
<Td align = "center"> <span id = "i3"> </span> </td>
</Tr>
<Tr class = "tr_cls">
<Td align = "center"> <span id = "a4"> </span> </td>
<Td align = "center"> <span id = "b4"> </span> </td>
<Td align = "center"> <span id = "c4"> </span> </td>
<Td align = "center"> <span id = "d4"> </span> </td>
<Td align = "center"> <span id = "e4"> </span> </td>
<Td align = "center"> <span id = "f4"> </span> </td>
<Td align = "center"> <span id = "g4"> </span> </td>
<Td align = "center"> <span id = "h4"> </span> </td>
<Td align = "center"> <span id = "i4"> </span> </td>
</Tr>
<Tr class = "tr_cls">
<Td align = "center"> <span id = "a5"> </span> </td>
<Td align = "center"> <span id = "b5"> </span> </td>
<Td align = "center"> <span id = "c5"> </span> </td>
<Td align = "center"> <span id = "d5"> </span> </td>
<Td align = "center"> <span id = "e5"> </span> </td>
<Td align = "center"> <span id = "f5"> </span> </td>
<Td align = "center"> <span id = "g5"> </span> </td>
<Td align = "center"> <span id = "h5"> </span> </td>
<Td align = "center"> <span id = "i5"> </span> </td>
</Tr>
<Tr class = "tr_cls">
<Td align = "center"> <span id = "a6"> </span> </td>
<Td align = "center"> <span id = "b6"> </span> </td>
<Td align = "center"> <span id = "c6"> </span> </td>
<Td align = "center"> <span id = "d6"> </span> </td>
<Td align = "center"> <span id = "e6"> </span> </td>
<Td align = "center"> <span id = "f6"> </span> </td>
<Td align = "center"> <span id = "g6"> </span> </td>
<Td align = "center"> <span id = "h6"> </span> </td>
<Td align = "center"> <span id = "i6"> </span> </td>
</Tr>
<Tr class = "tr_cls">
<Td align = "center"> <span id = "a7"> </span> </td>
<Td align = "center"> <span id = "b7"> </span> </td>
<Td align = "center"> <span id = "c7"> </span> </td>
<Td align = "center"> <span id = "d7"> </span> </td>
<Td align = "center"> <span id = "e7"> </span> </td>
<Td align = "center"> <span id = "f7"> </span> </td>
<Td align = "center"> <span id = "g7"> </span> </td>
<Td align = "center"> <span id = "h7"> </span> </td>
<Td align = "center"> <span id = "i7"> </span> </td>
</Tr>
<Tr class = "tr_cls">
<Td align = "center"> <span id = "a8"> </span> </td>
<Td align = "center"> <span id = "b8"> </span> </td>
<Td align = "center"> <span id = "c8"> </span> </td>
<Td align = "center"> <span id = "d8"> </span> </td>
<Td align = "center"> <span id = "e8"> </span> </td>
<Td align = "center"> <span id = "f8"> </span> </td>
<Td align = "center"> <span id = "g8"> </span> </td>
<Td align = "center"> <span id = "h8"> </span> </td>
<Td align = "center"> <span id = "i8"> </span> </td>
</Tr>
</Table>
</Form>
</Body>
</Html>
 
Use prototype to replace the script code.
The Code is as follows:
<Script type = "text/javascript" src = "prototype. js"> </script>
<Script type = "text/javascript">...
Function ajaxRequest ()...{
Var myAjax = new Ajax. Request (
'Http: // response ',
...{
Method: 'get ',
OnComplete: setData
}
);
}
Function setData (response )...{
Var contents = response. responseText;
Var stocks = contents. split (';');
For (var I = 0; I <stocks. length-1; I ++ )...{
Var content = stocks [I];
Var temp1 = content. split ('=') [0];
Var temp2 = content. split ('=') [1];
Var code = temp1.substr (temp1.length-6, 6 );
Var temp3 = temp2.replace ('"','');
Var name = temp3.split (',') [0];
Var tday_f = temp3.split (',') [1];
Var yest_f = temp3.split (',') [2];
Var curr_f = temp3.split (',') [3];
Var temp_f = curr_f-yest_f;
$ ('A' + I). innerHTML = code;
$ ('B' + I). innerHTML = name;
$ ('C' + I). innerHTML = curr_f;
If (curr_f> yest_f )...{
$ ('C' + I). innerHTML = "<font color = 'red'>" + curr_f + "</font> ";
} Else if (curr_f <yest_f )...{
$ ('C' + I). innerHTML = "<font color = 'green'>" + curr_f + "</font> ";
} Else ...{
$ ('C' + I). innerHTML = curr_f;
}
$ ('D' + I). innerHTML = tday_f;
$ ('E' + I). innerHTML = yest_f;
If (temp_f> 0 )...{
$ ('F' + I). innerHTML = "<font color = 'red'>" + temp_f.toFixed (2) + "</font> ";
$ ('G' + I ). innerHTML = "<font color = 'red'>" + (temp_f/yest_f) * 100 ). toFixed (2) + "</font> % ";
} Else if (temp_f <0 )...{
$ ('F' + I). innerHTML = "<font color = 'green'>" + temp_f.toFixed (2) + "</font> ";
$ ('G' + I ). innerHTML = "<font color = 'green'>" + (temp_f/yest_f) * 100 ). toFixed (2) + "</font> % ";
} Else ...{
$ ('F' + I). innerHTML = temp_f.toFixed (2 );
$ ('G' + I). innerHTML = (temp_f/yest_f) * 100). toFixed (2) + "% ";
}
$ ('H' + I). innerHTML = temp3.split (',') [4];
$ ('I' + I). innerHTML = temp3.split (',') [5];
}
}
Function pageInit ()...{
Windows. setInterval ("ajaxRequest ()", 3000 );
}
</Script>

Of course, the page can be automatically generated by the script, as well as the dynamic addition and deletion of Stock Information on the page, and so on, just wait for future modification.

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.