javascript,如何实现索引页函数来执行单独的.js函数页

javascript,如何实现索引页函数来执行单独的.js函数页,javascript,forms,function,calendar,Javascript,Forms,Function,Calendar,我创建了两个函数来执行这些任务: function displayBanner(currentDate) { /* get the month from current Date */ /* Set the imgsource variable to be the defaultLogo image */ /* If month is 12, 1, or 2, set variable imgsource to wi

我创建了两个函数来执行这些任务:

function displayBanner(currentDate) {
    /* get the month from current Date */

    /* Set the imgsource variable to be the defaultLogo
            image */

    /* If month is 12, 1, or 2, set variable imgsource to
              winterLogo or to the defaultLogo if not one of those 
              three months */

    /* If month is 3, 4, or 5, set imgsource to springLogo 
              or to the defaultLogo if not one of those three
              months */

    /* If month is 6, 7, or 8, set imgsource to summerLogo 
              or to the defaultLogo if not one of those three
              months */

    /* If month is 10, 11, or 12, set imgsource to fallLogo 
              or to the defaultLogo if not one of those three
              months */

    /* Return the imagesource variable to set the myBanner 
              imgSrc attribute to that image*/
}

function calcDaysToSale(currentDate) {
    /* create a Date object for the 15th of the 
              current month */
    /* compute difference in days between currentDate
              And the 15th */
    /* if the difference is positive return that value
              Else return message that the sale is over for this 
              month */
}
然而!我打算创建第三个函数,它可以驻留在索引页的标题中,随后可以执行这些函数,而问题就出在这里——我想不出来

我已经使用“alert();”检查了我的工作功能,这表明我创建的两个的操作是合理的,但我就是不能确定如何使他们的行为结合起来

页面如下所示:

<script type="text/javascript" src="flowers.js"></script>
<script type="text/javascript">


var curdate = new Date()
alert(curdate);

function displayBanner(currenttDate) {
    var munf = currenttDate.getMonth();

    switch (munf) {
    case 9:
    case 8:
    case 10:
        imageSrc = 'fallLogo.gif';
        break;
    case 11:
    case 0:
    case 1:
        imageSrc = 'winterLogo.gif';
        break;
    case 2:
    case 3:
    case 4:
        imageSrc = 'springLogo.gif';
        break;
    case 7:
    case 5:
    case 6:
        imageSrc = 'summerLogo.gif';
        break;
    default:
        imageSrc = 'defaultLogo.gif';
    }

    return imageSrc;
}


function calcDaysToSale(currentDate) {

    var saleMessage;
    var mday = currentDate.getDate()

    if (currentDate < 15) {
        var lef = (15 - currentDate);
        //alert("There are " + lef + " days until the Sale.");
        saleMessage = lef;
    } else if (currentDate == 15) {
        //alert("The Sale is today!");
        saleMessage = "Zero, it's Today!";
    } else {
        //alert("The Sale for this month has ended.");
        saleMessage = "The Sale for this month has ended.";
    }

    return saleMessage;
}

alert(calcDaysToSale(curdate));
alert(displayBanner(curdate));

function pageSetup() {
}



 </script>

</head>

<body  class="oneColLiqCtrHdr">

<div id="container">
<div id="header">
<p><img name="myBanner" id="myBanner" src="" alt="Carol's Flowers" />

  <!-- end #header -->
</p>
<div id="links"><a href="#">Home</a> | <a href="#">General Arrangements</a> | 
<a href="#">Seasonal Designs</a> | <a href="#">Custom Orders</a> | 
<a href="#">Location</a></div>

</div>
<div id="mainContent">
<table id="mainTable">
<tr>
   <td width="201"><h1><img src="Flowers.JPG" alt="Random Flowers" width="200"   height="255" /></h1></td>
   <td width="687">
<p>Here at Carol's Flowers, we believe that there is a perfect floral arrangment for  every occasion! Take a look around and see if there is anything you like. If we don't  already have it, then we will create it for you!</p></td>
</tr>
</table>
<!-- end #mainContent --></div>
<div id="footer">
<p> <form name="sale" id="sale">
  Days Until Mid Month Madness Sale : 
  <input type="text" name="saleMessage" id="saleMessage" /></form></p>


<!-- end #footer --></div>
<!-- end #container --></div>
</body>
</html>

var curdate=新日期()
警报(curdate);
函数显示横幅(currenttDate){
var munf=currentDate.getMonth();
开关(munf){
案例9:
案例8:
案例10:
imageSrc='fallLogo.gif';
打破
案例11:
案例0:
案例1:
imageSrc='winterLogo.gif';
打破
案例2:
案例3:
案例4:
imageSrc='springLogo.gif';
打破
案例7:
案例5:
案例6:
imageSrc='summerLogo.gif';
打破
违约:
imageSrc='defaultLogo.gif';
}
返回图像src;
}
函数calcDaysToSale(当前日期){
var-saleMessage;
var mday=currentDate.getDate()
如果(当前日期<15){
var lef=(15-当前日期);
//警报(“离销售还有“+lef+”天”);
saleMessage=lef;
}否则如果(当前日期==15){
//警惕(“今天减价!”);
saleMessage=“零,今天到了!”;
}否则{
//警惕(“本月的销售已结束。”);
saleMessage=“本月的销售已结束。”;
}
返回消息;
}
警报(calcDaysToSale(curdate));
警报(显示横幅(curdate));
函数页面设置(){
}

| | | | 在卡罗尔花店,我们相信每一个场合都有一个完美的花艺布置!看看周围有没有你喜欢的东西。如果我们还没有,那么我们将为您创建它

月中疯狂销售前的天数:

但正如我所说的,我打算删掉前两个函数,将它们放入单独的“flowers.js”页面,并使用pagesetup()函数运行所有内容


这两个功能的目标是一方面设置横幅的图像源,另一方面在底部表单中显示“销售”的剩余天数,该销售发生在每月15日,或者如果15日已经过去,则让用户知道该信息

虽然你的问题不是很清楚。让我试一试。因此,您将这两个函数移动到了一个新的js文件“flowers.js”。此页面包含flowers.js,现在尝试从页面上存在的pageSetup方法调用这两个函数。你就是这么想的吗?没用吗

如果没有,那么您需要确保“flowers.js”已经完成加载,然后才能调用它的函数。检查这是否导致问题的一种方法是仅在页面加载完成后将代码更改为调用pageSetup

<body onload="pageSetup();">

如果您的问题完全不同,请告诉我。:)

编辑:
要使用javascript在运行时设置图像源,可以执行以下操作

<img id="my_image_id" src="Flowers.JPG" alt="Random Flowers" width="200"   height="255" />

<script type="text/javascript">
    function changeImage(a) {
        document.getElementById("my_image_id").src=displayBanner(10);
    }
</script>

功能更改图像(a){
document.getElementById(“我的图片id”).src=displayBanner(10);
}
附言:

重命名displayBanner以获取_image_source或其他东西。

ECMAScript/Javscript与Java不同。您能用三行代码解释一下您想要实现的目标吗?保持简单。。如果你这样做了,我会帮助你,如果你把这些函数放到另一个js文件中,到底发生了什么?你有错误吗?你的问题有点模糊,但我承认你似乎意识到了这一点;我至少明白,这个页面似乎并没有做你希望它做的事情。至少,您知道大多数现代浏览器提供的调试工具吗?您仍然需要在较旧的浏览器中进行测试,但如果您打开Google Chrome,然后按F12,然后单击“脚本”选项卡,您实际上可以设置断点,为自己提供比在不同位置使用“alert()”更好的信息。它还有一个控制台,可以报告诸如“undefined”之类的错误。尽管你的问题不是很清楚。让我试一试。因此,您将这两个函数移动到了一个新的js文件“flowers.js”。此页面包含flowers.js,现在尝试从页面上存在的pageSetup方法调用这两个函数。你就是这么想的吗?没用吗?虽然你的问题不是很清楚。让我试一试。因此,您将这两个函数移动到了一个新的js文件“flowers.js”。此页面包含flowers.js,现在尝试从页面上存在的pageSetup方法调用这两个函数。大家好,这完全正确。我有这两个功能,它们似乎可以工作,但我想将它们从索引页面的标题中取出,并将它们传输到单独的flowers.js页面,然后通过第三个功能(位于原始索引页面中)pagesetup()使用它们。另一件事我不太确定如何从该函数中准确设置图像源->是否有必要深入到代码中,直到图像执行完毕,然后再添加一部分来执行图像设置?文本框是一样的,我不太知道如何将“Sale”函数的结果写入该文本框。谢谢你耐心地阅读这篇文章,并试图帮助我解决这个问题。我为我可笑的无知道歉。我真诚地感谢大家的关心。