最近,有人向我反映,用Firefox访问博客园,无法使用搜索,输入搜索内容后无法提交。博客园的搜索功能是通过javascript提交的,代码如下:


Firefox中Javascript使用event对象需要注意的问题function SearchGoogle(key,evt)
{
Firefox中Javascript使用event对象需要注意的问题            
if(event.keyCode==13 || event.keyCode==0)
{
Firefox中Javascript使用event对象需要注意的问题                
var keystr = encodeURIComponent(key.value);
Firefox中Javascript使用event对象需要注意的问题                url 
= "http://www.google.com/search?q=";
Firefox中Javascript使用event对象需要注意的问题                url 
= url+keystr;
Firefox中Javascript使用event对象需要注意的问题                url 
+= "&ie=UTF-8&oe=GB2312&hl=zh-CN&domains=www.cnblogs.com&sitesearch=www.cnblogs.com";
Firefox中Javascript使用event对象需要注意的问题                window.location
=url;
Firefox中Javascript使用event对象需要注意的问题                
Firefox中Javascript使用event对象需要注意的问题            }

Firefox中Javascript使用event对象需要注意的问题        }

Firefox中Javascript使用event对象需要注意的问题
</script>
Firefox中Javascript使用event对象需要注意的问题
<H2>Google站内搜索</H2>
Firefox中Javascript使用event对象需要注意的问题
<h4><input style="WIDTH: 130px" type="text" name="q" onkeydown="SearchGoogle(q)">&nbsp;<input onclick="SearchGoogle(q)" type="button" value="搜索" name="sa">
Firefox中Javascript使用event对象需要注意的问题
</h4>


这样的代码在IE中可以正常运行,但在Firefox中却出现event is not defined的错误。
因为在Firefox中使用了不同的事件对象模型,不同于IE Dom,用的是W3C Dom。
解决方法请看如下代码:


Firefox中Javascript使用event对象需要注意的问题function SearchGoogle(key,evt)
{
Firefox中Javascript使用event对象需要注意的问题            
if(evt.keyCode==13 || evt.keyCode==0)
{
Firefox中Javascript使用event对象需要注意的问题                
var keystr = encodeURIComponent(key.value);
Firefox中Javascript使用event对象需要注意的问题                url 
= "http://www.google.com/search?q=";
Firefox中Javascript使用event对象需要注意的问题                url 
= url+keystr;
Firefox中Javascript使用event对象需要注意的问题                url 
+= "&ie=UTF-8&oe=GB2312&hl=zh-CN&domains=www.cnblogs.com&sitesearch=www.cnblogs.com";
Firefox中Javascript使用event对象需要注意的问题                window.location
=url;
Firefox中Javascript使用event对象需要注意的问题                
return;
Firefox中Javascript使用event对象需要注意的问题            }

Firefox中Javascript使用event对象需要注意的问题        }

Firefox中Javascript使用event对象需要注意的问题
</script>
Firefox中Javascript使用event对象需要注意的问题
<H2>Google站内搜索</H2>
Firefox中Javascript使用event对象需要注意的问题
<h4><input style="WIDTH: 130px" type="text" name="q" onkeydown="SearchGoogle(q,event)">&nbsp;<input onclick="SearchGoogle(q,event)" type="button" value="搜索" name="sa">
Firefox中Javascript使用event对象需要注意的问题
</h4>


相关文章: