What is "Object Expected" error in Internet Explorer?

If an ad shows up properly in Firefox, Safari, Opera but it shows an error "Object Expected" in Internet Explorer, you need to verify the ad code. This error usually happens in the following scenario. An HTML file contains an inline JavaScript to load remote.js and call a function in remote.js

 1 <script text="text/javascript">
 2 <![CDATA[
 3 alert('Before loading remote.js');
 4 document.write('<sc'+'ript type="text/javascript" src="remote.js"><\/script>');
 5 alert('After loading remote.js');
 6  
 7 // call a remote function that should be already be declared
 8 document.write('<sc'+'ript type="text/javascript">remoteFunction();<\/script>');
 9 ]]>
10 </script>

And in the file remote.js:

1 function remoteFunction() {
2    alert('In remote function');
3 }

You would expect the output in this order:

  1. Before loading remote.js
  2. After loading remote.js
  3. In remote function

IE gives an error message "Object Expected" because it calls the function remoteFunction() prematurely. It calls before loading the file remote.js, which contain the declaration of remoteFunction(). This is another unexpected behavior from IE. Fortunately, it can be corrected by adding the attribute defer="defer" to second script invocation. This will specifically prevent IE from executing in order to give a correct output. Other browsers are not affected by the change.

e.g.

<script type="text/javascript" src="demo_defer.js" defer="defer"></script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值