Considering that using dynamic pages on the homepage affects server performance, JavaScript is used to dynamically update the homepage.
Principle: Take press publication as an example. When a piece of news is sent in the background, an XML file is dynamically generated by accessing the database to save news information. When the front-end page is opened, use JavaScript to read and display XML files.
BackgroundCode:
Sseentities _ DB = new sseentities ();
VaR newstoview = (from N in _ dB. News select N). toarray (). Reverse (). tolist ();
Xmldocument XD = new xmldocument ();
Xmldeclaration xde;
Xde = XD. createxmldeclaration ("1.0", "GBK", null );
XD. appendchild (xde );
Xmlelement newslist = XD. createelement ("newslist ");
XD. appendchild (newslist );
Xmlnode root = XD. selectsinglenode ("newslist ");
Xmlelement [] news = new xmlelement [4];
Xmlelement [] Text = new xmlelement [4];
Xmlelement [] IMG = new xmlelement [4];
For (INT I = 0; I <4; I ++)
{
News [I] = XD. createelement ("news ");
Text [I] = XD. createelement ("text ");
IMG [I] = XD. createelement ("IMG ");
News [I]. setattribute ("ID", I. tostring ());
If (I <newstoview. Count)
{
Text [I]. innertext = newstoview [I]. title;
IMG [I]. innertext = "content/newsimages/slider_item1.png ";
}
Else
{
Text [I]. innertext = "";
IMG [I]. innertext = "";
}
News [I]. appendchild (Text [I]);
News [I]. appendchild (IMG [I]);
Root. appendchild (news [I]);
}
XD. Save (server. mappath ("../sources/news. xml "));
Front-end read code:
<SCRIPT type = "text/JavaScript">
Function loadxml (xmlfile ){
VaR xmldoc; Container Freight
If (! Window. activexobject ){
VaR parser = new domparser ();
Xmldoc = parser. parsefromstring (xmlfile, "text/XML ");
} Else {
Xmldoc = new activexobject ("Microsoft. xmldom ");
Xmldoc. async = "false ";
Xmldoc. Load (xmlfile );
}
Return xmldoc; iphone5
}
VaR xmldoc = loadxml ("sources/news. xml ");
// Parse the XML file and determine whether an error occurs.
If (xmldoc. parseerror. errorcode! = 0 ){
Alert (xmldoc. parseerror. Reason );
}
// Obtain the root node
VaR nodes = xmldoc.doc umentelement. childnodes;
</SCRIPT>