谢谢 barabis 解决方案 在************ @ gmail.com 说: 但是当我尝试将dateObj变量放在函数外部(带有或没有前面的var)时,它告诉我dateObj没有定义。 Aren''t变量声明在一个应该是全局的函数之外?它在IE 5中工作正常顺便 < script type =" text / javascript&quo t;> var dateObj = new Date(); 函数createCal(){ for(var i = 0; i< 32; i ++) { date = dateObj.getDate(); set_date = dateObj.setDate(i); document.write(date); } } < / script> 有趣的是,当我用一个警告替换document.write()时,一切正常。 这是关于如何/何时调用你的函数createCal()的问题。 如果在页面之后调用它已经渲染,第一次调用 of document.write()清除当前页面,摧毁你的全局 变量。 它不会影响IE,这很有趣。难怪很多dhtml日历 不能在firefox中工作。 " Lee" < RE ************** @ cox.net>在消息中写道 news:cu ********* @ drn.newsguy.com ... 在************ @ gmail.com 说: 但是当我尝试将dateObj变量放在函数外部(使用或没有前面的var)时,它告诉我dateObj未定义。在函数外部声明的变量应该是全球性的吗?这一切在IE 5中运行正常 < script type =" text / javascript"> var dateObj = new Date(); 函数createCal(){ for(var i = 0; i< 32; i ++){ date = dateObj.getDate(); set_date = dateObj.setDate(i); document.write(date); } } < /脚本> 有趣的是,当我用一个警告替换document.write()时,一切都按预期工作。 这是一个问题您正在调用函数createCal()的方式/时间ed。如果在页面渲染后调用它,document.write()的第一次调用将清除当前页面,破坏您的全局变量。 ******** **** @ gmail.com 写道:我希望有人可以指出我的错误,因为我开始失去我的头发在此。这可能是一个非常直截了当的错误,但在仅仅4个小时的睡眠之后它正在努力。它与Firefox 1中的全局变量有关。 不,这就是李所说的。 操纵字符串比日期对象快很多。请看下面的代码 ,它会生成任意月份的日期,给出一年和一个月的b $ b。它使用日期对象和普通的 字符串方法计算方法。在我古老的笔记本电脑上,日期对象方法 一直需要60到70毫秒,字符串方法需要大约10毫秒。 我有想法试试这个J博士的网站比较了 验证用户输入的日期字符串的方法: < URL:http://www.merlyn .demon.co.uk / js-date4.htm #TDVal> 虽然代码有点长,但整体效果是 3倍提高速度。 < script type =" text / javascript"> function createCal0(sMonth){ var o = document.getElementById(''writeSpan''); var m = sMonth.split(/ \ D + /); m [1] - ; //调整月份数 var dateObj = new Date(m [0],m [1]); var t; //只使用''cos appendChild行太长了 while(m [1] == dateObj.getMonth()){ t = addZ( dateObj.getDate()); o.appendChild(document.createTextNode(t +'','')); dateObj.setDate(dateObj.getDate() +1) } } 函数createCal1(sMonth){ var o = document.getElementById(''writeSpan''); var m = sMonth.split(/ \ D + /); var limit; var ds = []; if(m [1] == 4 || m [1] == 6 || m [1] == 9 || m [1] =如果(m [1] == 1 || m [1] == 3 || m [1] == 5 || m [1] == 7 || m [1] == 8 || m [1] == 10 || m [1] == 12){ limit = 31; }否则{ limit = 28; if(m [0]%4 == 0)limit = 29; if(m [0]%100 == 0)limit = 28; if(m [0]%400 == 0)limit = 29; } for(var i = 1; i< = limit; i ++){ ds.push( addZ(i)); } o.innerHTML = ds.join('',''); } 函数addZ(x){ return(x< 10)?''0''+ x:x; } < / script> < form> ; < input name =" ym" value =" 2005-02">输入一年 和月(yyyy-mm)< br> < button onclick =" var s = new Date(); createCal0(this.form.ym.value); var f = new Date(); var t = f - s; alert(''花了''+ t +''ms''); 返回false; ">写日期0< / button> < br> < button onclick =" var s = new Date(); createCal1(this.form.ym.value); var f = new Date() var t = f - s; alert(''花了''+ t +''ms''); 返回false; ">写日期1< / button> < br> < / form> < span id =" writeSpan">< / span> - Rob Hi,I hope someone can point out my error because I''m starting to lose myhair over this. It''s probably a very straigh forward error but afteronly 4 hours sleep it''s doing my head in. It''s to do with globalvariables in Firefox 1. The following code works no problems and generates a whole bunch ofnumbers <script type="text/javascript">function createCal(){var dateObj = new Date();for(var i=0;i<32;i++){date = dateObj.getDate(); set_date = dateObj.setDate(i);document.write(date); } }</script> but when I try to place the dateObj variable outside the function (withor without the preceding "var") it tells me dateObj is not defined.Aren''t variables declared outside of a function supposed to be global?It all works fine in IE 5 btw <script type="text/javascript"> var dateObj = new Date();function createCal(){ for(var i=0;i<32;i++){date = dateObj.getDate(); set_date = dateObj.setDate(i);document.write(date); } }</script> Interestingly when I replace the document.write() with an alerteverything works as it should. Thanks barabis 解决方案 in************@gmail.com said: but when I try to place the dateObj variable outside the function (withor without the preceding "var") it tells me dateObj is not defined.Aren''t variables declared outside of a function supposed to be global?It all works fine in IE 5 btw<script type="text/javascript">var dateObj = new Date();function createCal(){ for(var i=0;i<32;i++){date = dateObj.getDate();set_date = dateObj.setDate(i);document.write(date);}}</script>Interestingly when I replace the document.write() with an alerteverything works as it should. It''s a matter of how/when your function createCal() is being called.If it''s called after the page has been rendered, the first invocationof document.write() clears the current page, destroying your globalvariables. It''s funny that it doesnt affect IE. No wonder a lot of the dhtml calendarsdon''t work in firefox tho."Lee" <RE**************@cox.net> wrote in messagenews:cu*********@drn.newsguy.com... in************@gmail.com said:but when I try to place the dateObj variable outside the function (withor without the preceding "var") it tells me dateObj is not defined.Aren''t variables declared outside of a function supposed to be global?It all works fine in IE 5 btw<script type="text/javascript">var dateObj = new Date();function createCal(){ for(var i=0;i<32;i++){ date = dateObj.getDate(); set_date = dateObj.setDate(i); document.write(date); }}</script>Interestingly when I replace the document.write() with an alerteverything works as it should. It''s a matter of how/when your function createCal() is being called. If it''s called after the page has been rendered, the first invocation of document.write() clears the current page, destroying your global variables.in************@gmail.com wrote: Hi, I hope someone can point out my error because I''m starting to lose my hair over this. It''s probably a very straigh forward error but after only 4 hours sleep it''s doing my head in. It''s to do with global variables in Firefox 1. No, it''s what Lee said. Manipulating strings is a lot faster than date objects. Have alook at the code below, it generates dates for any month given ayear and month. It times methods using date objects and plainstring methods. On my ancient laptop, the date object methodconsistently took 60 to 70ms, the string method took around 10ms. I got the idea to try this from Dr. J''s site where he comparesmethods of validating user-entered date strings: <URL:http://www.merlyn.demon.co.uk/js-date4.htm#TDVal> Whilst the code is somewhat longer, the overall effect is a3-fold increase in speed.<script type="text/javascript">function createCal0(sMonth){ var o = document.getElementById(''writeSpan'');var m = sMonth.split(/\D+/);m[1]--; // Adjust month numbervar dateObj = new Date(m[0],m[1]);var t; // Only used ''cos appendChild line was too long while (m[1] == dateObj.getMonth()){t = addZ(dateObj.getDate());o.appendChild(document.createTextNode(t + '',''));dateObj.setDate(dateObj.getDate()+1)}} function createCal1(sMonth){var o = document.getElementById(''writeSpan'');var m = sMonth.split(/\D+/);var limit;var ds = [];if ( m[1]==4 || m[1]==6 || m[1]==9 || m[1]==10) {limit = 30;} else if (m[1]==1 || m[1]==3 || m[1]==5|| m[1]==7 || m[1]==8 || m[1]==10 || m[1]==12){limit = 31;} else {limit = 28;if ( m[0]%4 == 0 ) limit = 29;if ( m[0]%100 == 0 ) limit = 28;if ( m[0]%400 == 0 ) limit = 29;}for (var i=1; i<=limit; i++) {ds.push(addZ(i));}o.innerHTML = ds.join('','');} function addZ(x){return (x<10)?''0''+x:x;}</script><form><input name="ym" value="2005-02">Enter a yearand month (yyyy-mm)<br><button onclick="var s = new Date();createCal0(this.form.ym.value);var f = new Date();var t = f - s;alert(''That took '' + t + '' ms'');return false;">write dates 0</button><br><button onclick="var s = new Date();createCal1(this.form.ym.value);var f = new Date()var t = f - s;alert(''That took '' + t + '' ms'');return false;">write dates 1</button><br></form><span id="writeSpan"></span>--Rob 这篇关于firefox全局变量困境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 13:10