最近,有人向我反映,用Firefox访问博客园,无法使用搜索,输入搜索内容后无法提交。博客园的搜索功能是通过javascript提交的,代码如下: < script language ="JavaScript" > function SearchGoogle(key,evt) { if(event.keyCode==13 || event.keyCode==0) { var keystr = encodeURIComponent(key.value); url = "http://www.google.com/search?q="; url = url+keystr; url += "&ie=UTF-8&oe=GB2312&hl=zh-CN&domains=www.cnblogs.com&sitesearch=www.cnblogs.com"; window.location=url; } } </ script > < H2 > Google站内搜索 </ H2 > < h4 >< input style ="WIDTH: 130px" type ="text" name ="q" onkeydown ="SearchGoogle(q)" > < input onclick ="SearchGoogle(q)" type ="button" value ="搜索" name ="sa" > </ h4 > 这样的代码在IE中可以正常运行,但在Firefox中却出现event is not defined的错误。 因为在Firefox中使用了不同的事件对象模型,不同于IE Dom,用的是W3C Dom。 解决方法请看如下代码: < script language ="JavaScript" > function SearchGoogle(key,evt) { if(evt.keyCode==13 || evt.keyCode==0) { var keystr = encodeURIComponent(key.value); url = "http://www.google.com/search?q="; url = url+keystr; url += "&ie=UTF-8&oe=GB2312&hl=zh-CN&domains=www.cnblogs.com&sitesearch=www.cnblogs.com"; window.location=url; return; } } </ script > < H2 > Google站内搜索 </ H2 > < h4 >< input style ="WIDTH: 130px" type ="text" name ="q" onkeydown ="SearchGoogle(q,event)" > < input onclick ="SearchGoogle(q,event)" type ="button" value ="搜索" name ="sa" > </ h4 >