惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 雪无止境

WEB 架构布置 YouTube的系统架构 [原创]如何得到网站的最后更新日期 首例全国性电子商务规范将出台 安装Microsoft VS2003“安装程序检测到另一个程序要求计算机重新启动 ASP.Net 注册组件 如何用正确的方法来写出质量好的软件的75条体会 ASP.NET 生成图形验证码 (C#版本) [原创]让C#驱动机器,为我们奏响电子音乐 [原创]在ASP.NET中为图形加水印的方法 (C#版本) IE 与 FireFox 的 showModalDialog 用户 'sa' 登录失败。原因: 未与信任 SQL Server 连接相关联 常用SQL ASP获取即时ID值(自动编号) ASP ADOX 使用范例 [原创]重载、继承 [原创]ASP开发工具论 [原创]用Data Shape 来取关系数据库的多表记录 [原创]基于角色的Roles
Ajax 及 JavaScript 在各浏览器下的兼容性问题
雪无止境 · 2008-04-27 · via 博客园 - 雪无止境

如何使用JavaScript 建立起IE或Mozilla浏览器相应的请求对象。客户端使用的浏览器是各种各样的。因此也有不同的请求对象。如在Firefox, Netscape, Safari,Opera中是XMLHttpRequest。IE则是Microsoft.XMLHTTP 或 Msxml2.XMLHTTP。

使用AJAX的第一步是检测客户浏览器的类型,根据相应的类型取得request 对象。

/* Initialize a Request object that is already constructed /
function initReq(reqType,url,bool){
/ Specify the function that will handle the HTTP response 
*/

request.onreadystatechange
=handleResponse; 
/*
 onreadystatechange监听handleResponse函数。接下来就是打开连接,发送请求……
*/

request.open(reqType,url,bool);
request.send(
null);
}
/*
 Wrapper function for constructing a Request object.
Parameters:
reqType: The HTTP request type such as GET or POST.
url: The URL of the server program.
asynch: Whether to send the request asynchronously or not. 
*/
function httpRequest(reqType,url,asynch){
//Mozilla-based browsers

if(window.XMLHttpRequest){    //如果是非IE浏览器
request = new XMLHttpRequest(); //取得对象。
initReq(reqType,url,asynch);
else if (window.ActiveXObject){ //如果是IE浏览器

request=new ActiveXObject("Msxml2.XMLHTTP");
if (!
 request){
request
=new ActiveXObject("Microsoft.XMLHTTP"
);
}
if
(request){
initReq(reqType,url,asynch); 
/*
 Unlikely to branch here, as IE users will be able to use either one of the 
constructors
*/

else {
alert(
"Your browser does not permit the use "+

"of all of this application's features!");}
else
 {
alert(
"Your browser does not permit the use "+

"of all of this application's features!");}
}

在JavaScript中,还有一些需要注意的内容,在各各浏览器下是有区别的。

1.document.formName.item("itemName") 问题

说明:IE下,可以使用document.formName.item(

"itemName")或document.formName.elements["elementName"];Firefox下,只能使用

document.formName.elements[

"elementName"
]. 
解决方法:统一使用document.formName.elements[
"elementName"
]. Text1: 
document.formName.item(
"itemName"
)         
document.formName.elements[
"elementName"
-------------------------------------------------
2.集合类对象问题

说明:IE下,可以使用()或[]获取集合类对象;Firefox下,只能使用[]获取集合类对象. 
解决方法:统一使用[]获取集合类对象.
Text2: 
document.forms(

"formName"
)  
document.forms[
"formName"
]  

Text3: 
document.getElementsByName(

"inputName")(0
)  
document.getElementsByName(
"inputName")[0
-------------------------------------------------
3.自定义属性问题

说明:IE下,可以使用获取常规属性的方法来获取自定义属性,也可以使用getAttribute()获取自定义属性;Firefox下,只能使用

getAttribute()获取自定义属性. 
解决方法:统一通过getAttribute()获取自定义属性.
Text4: 
直接获取自定义属性的值                
通过getAttribute()获取自定义属性的值 

-------------------------------------------------

4.eval("idName")问题

说明:IE下,,可以使用eval(

"idName")或getElementById("idName"
)来取得id为idName的HTML对象;Firefox下只能使用

getElementById(

"idName"
)来取得id为idName的HTML对象. 
解决方法:统一用getElementById(
"idName"
)来取得id为idName的HTML对象.
Text5: 
eval(
"idName"
)                     
document.getElementById(
"itemId"
-------------------------------------------------
5.变量名与某HTML对象ID相同的问题

说明:IE下,HTML对象的ID可以作为document的下属对象变量名直接使用;Firefox下则不能.Firefox下,可以使用与HTML对象ID相同的

变量名;IE下则不能。
解决方法:使用document.getElementById(

"idName"
)代替document.idName.最好不要取HTML对象ID相同的变量名,以减少错误;在声明

变量时,一律加上var,以避免歧义. 

-------------------------------------------------

6.const问题

说明:Firefox下,可以使用const关键字或var关键字来定义常量;IE下,只能使用var关键字来定义常量. 
解决方法:统一使用var关键字来定义常量.

-------------------------------------------------

7.input.type属性问题

说明:IE下input.type属性为只读;但是Firefox下input.type属性为读写.

-------------------------------------------------

8.window.event问题

说明:window.event只能在IE下运行,而不能在Firefox下运行,这是因为Firefox的event只能在事件发生的现场使用. 
解决方法: 
IE: 

<input name="Button8_1" type="button" value="IE" onclick="javascript:gotoSubmit8_1()"/

 
<script language="javascript">
 
function
 gotoSubmit8_1() { 
 
alert(window.event); 
//use window.event 

 

</script> 

IE&Firefox: 
<input name="Button8_2" type="button" value="IE" onclick="javascript:gotoSubmit8_2(event)"/

 
<script language="javascript">
 
function
 gotoSubmit8_2(evt) { 
 
evt
=evt?evt:(window.event?window.event:null
);
alert(evt); 
//use evt

 

</script>


-------------------------------------------------
9.event.x与event.y问题

说明:IE下,even对象有x,y属性,但是没有pageX,pageY属性;Firefox下,even对象有pageX,pageY属性,但是没有x,y属性. 
解决方法:使用mX(mX 

= event.x ?
 event.x : event.pageX;)来代替IE下的event.x或者Firefox下的event.pageX.-------------------------------------------------
10.event.srcElement问题

说明:IE下,even对象有srcElement属性,但是没有target属性;Firefox下,even对象有target属性,但是没有srcElement属性. 
解决方法:使用obj(obj 

= event.srcElement ?
 event.srcElement : event.target;)来代替IE下的event.srcElement或者Firefox下

的event.target.

-------------------------------------------------

11.window.location.href问题

说明:IE或者Firefox2.

0.x下,可以使用window.location或window.location.href;Firefox1.5
.x下,只能使用window.location. 
解决方法:使用window.location来代替window.location.href.
-------------------------------------------------
12.模态和非模态窗口问题

说明:IE下,可以通过showModalDialog和showModelessDialog打开模态和非模态窗口;Firefox下则不能. 
解决方法:直接使用window.open(pageURL,name,parameters)方式打开新窗口。

如果需要将子窗口中的参数传递回父窗口,可以在子窗口中使用window.opener来访问父窗口. 例如:

var parWin =
 window.opener; 

parWin.document.getElementById(

"Aqing").value = "Aqing"
-------------------------------------------------
13.frame问题

以下面的frame为例:

<frame src="xxx.html" id="frameId" name="frameName" />


(
1)访问frame对象:
IE:使用window.frameId或者window.frameName来访问这个frame对象.
Firefox:只能使用window.frameName来访问这个frame对象.
另外,在IE和Firefox中都可以使用window.document.getElementById(
"frameId"
)来访问这个frame对象.

(

2
)切换frame内容:
在IE和Firefox中都可以使用window.document.getElementById(
"testFrame").src = "xxx.html"或window.frameName.location =
 "xxx.html"来切换frame的内容.

如果需要将frame中的参数传回父窗口,可以在frme中使用parent来访问父窗口。例如:

parent.document.form1.filename.value

="Aqing"
;-------------------------------------------------
14.body问题

Firefox的body在body标签没有被浏览器完全读入之前就存在;而IE的body则必须在body标签被浏览器完全读入之后才存在.

例如: 
Firefox: 

<body>
 
<script type="text/javascript">
 
document.body.onclick 
= function
(evt){ 
evt 
= evt ||
 window.event; 
alert(evt); 

</script> 

</body> 
IE&Firefox: 
<body>

</body> 
<script type="text/javascript"> 
document.body.onclick 
= function
(evt){ 
evt 
= evt ||
 window.event; 
alert(evt); 

</script>