开发者

Why my function only works once (doing a document.write() job)?

In my html file, I have to display charts of different stocks in different places in the document. I wrote a javascript function source(ticker) to display the chart of "ticker". Here is the first part of my html document:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="CLP_style.css" rel="stylesheet" type="text/css">
<title>The XXXXXX Daily</title>
<script type="text/javascript">
function source(ticker)
{
                var d=new Date();
                var month=new Array(12);
                month[0]="01";
                month[1]="02";
                month[2]="03";
                month[3]="04";
                month[4]="05";
                month[5]="06";
                month[6]="07";
                month[7]="08";
                month[8]="09";
                month[9]="10";
                month[10]="11";
                month[11]="12";
                var startmonth;
                var startyear;
                var endmonth;
                endmonth=month[d.getMonth()];
                switch (endmonth) 
                    {
                    case "01":
                        startmonth="11";
                    case "02":
                        startmonth="12";
                    case "03":
                        startmonth="01";
                    case "04":
        开发者_如何学Go                startmonth="02";
                    case "05":
                        startmonth="03";
                    case "06":
                        startmonth="04";
                    case "07":
                        startmonth="05";
                    case "08":
                        startmonth="06";
                    case "09":
                        startmonth="07";
                    case "10":
                        startmonth="08";
                    case "11":
                        startmonth="09";
                        startyear="2010";
                    case "12":
                        startmonth="10";
                        startyear="2010";
                    }
                var dateparam;
                dateparam=startyear+"-"+startmonth+"-"+d.getDate()+"&e="+d.getFullYear()+"-"+endmonth+"-"+d.getDate();
                var chartsource;
                chartsource="<img src=\"http://xxxxxx.com/chartsymbol_chart.php?s="+dateparam+"&m=line&dateby=1&bollinger=1&bollinger_day=20&sma=1&sma_day=20&chartsize=1&id="+ticker+"\"";
                source=chartsource+" align=\"top\" name=\"HSX\" height=\"250\" width=\"490\">";
                document.write(source);
}
            </script>
</head>

<body>

When I call the function, it works perfectly well.

<table width="980" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td>
            <script type="text/javascript"> 
                source("^vnindex");
            </script>
        </td>
        <td>
            <script type="text/javascript"> 
                source("^hastc");
            </script>
        </td>
  </tr>
</table>

but only the first time i.e ^vnindex chart show up, but ^hastc does not. At first, I thought it was because there is no ^hastc stock. But when I check with a simple html img tag, ^hastc did show up. And for the remaining part of the document, no chart shows up at all. It means that the function only work once.

I have no idea on how to fix it, and would appreciate it very much if you can help me with this.

Thanks Toan Nguyen


Bear in mind that document.write appends to the document only if the document is loading otherwise it will try and overwrite the contents of the page with whatever you specify! Try insteand the DOM approach: document.appendChild( createElement("img") )....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜