我的标题就是为了方便别人能够通过搜索来得到这篇文章。
环境:VS2005 C# windowsXP IE8 FireFox3.6.3
经过熬夜苦战,终于解决了自己论坛代码兼容IE和FireFox的问题。现将一些经验分享如下:
1.fireFox支不支持body onload事件?
网上好多人说在fireFox中body onload事件没反应,建议用window.onload代替。我的实战结果是:
fireFox支持body onload事件,以前说不支持的朋友可能是版本问题或代码有误。另外,window.onload也可以触发,但它不 能完全代替body onload!这是因为window.onload发生在窗体刚刚加载时,而body onload发生在页面加载后。我在window.onload事件函数中试图设置iframe对像属性,结果抛出对象为空异常。而把这些代码放body onload事件函数中则没问题。
2.firefox中iframe能不能置于可编辑状态
IE中是很容易的,FireFox中也可以,不过要类似用下面方式:
HTML code
<
html
>
<
head
>
</
head
>
<
script
language
="javascript"
>
<!--
function
start() { document.getElementById(
"
myiframe
"
).contentWindow.document.designMode
=
"
on
"
; }
-->
</
script
>
<
body
onLoad
="start()"
>
<
iframe
id
="myiframe"
style
="width:750px; height:400px; border:1;"
></
iframe
>
</
body
>
</
html
>
3 JS中如何引用iframe document对象:
经过实战,得出如下经验:
IE直接使用Iframe标签中的ID:iframeID.document.xxx
FireFox中使用: document.getElementById("iframeID").contentWindow.document.xxx
我还发现:IE中使用document.getElementById方法反而不行(仅对于iframe),这可能是我代码问题?
4 JS中如何让iframe自适应高度?
HTML code
<
script
type
="text/javascript"
language
="javascript"
>
//
这里也是为了满足IE和fireFox需求
function
stateChangeIE(_frame) {
if
(_frame.readyState
==
"
complete
"
)
//
state: loading ,interactive, complete
{ AutoHeight(); } }
function
stateChangeFirefox(_frame) { AutoHeight(); }
function
AutoHeight() {
if
(document.readyState
!=
'
complete
'
) { setTimeout(
function
(){AutoHeight();},
50
);
return
; }
else
{
try
{
//
IE、fireFox下测试通过
var
ifobj
=
document.getElementById(
"
oldhtml
"
); ifobj.style.height
=
ifobj.contentWindow.document.body.scrollHeight
+
20
+
"
px
"
; }
//
注意,别忘了加最后那个"px",不然fireFox下就不起作用了
//
加的那20是为了让高度稍富余些
catch
(e) {} } }
</
script
>
<
iframe
id
="oldhtml"
onreadystatechange
="stateChangeIE(this)"
onload
="stateChangeFirefox(this)"
scrolling
="yes"
style
="width: 120%; height: 9px"
width
="99%"
></
iframe
>
5 如何判断是否是IE浏览器?
世界各国高手们正不断使这个问题变得更简单,目前最新成果是仅用7个字节便完成判定,我找的这个方法虽然要多用几个字节,但经实战证明它是可行的:
if("\v"=="v") // 这个判断真的只有在IE下才为真,包括IE 8。
{IE}
else
{非IE}
6 JS中如何向iframe中写入html文本:
var html =
"<HTML><HEAD></HEAD>"+
"<BODY><span style=\"font-family:宋体; font-size:13px;\"><br><br><br><br><br><br><br><br></span></BODY></HTML>";
IE:iframeID.document.write(html);
FireFox:
document.getElementById("iframeID").contentWindow.document.write(html);
7 后台Page_Load和js里的body onload谁先执行?前者先执行(貌似弱智话题,可我开始就是不知道)
转载于:https://www.cnblogs.com/spark_wu/archive/2010/04/10/1709038.html
相关资源:防止页面被iframe(兼容IE,Firefox火狐)