开发者

"Unexpected call to method or property access." in IE with appendChild()

function fload(){
  f = document.createElement('iframe');
  f.setAttribute('id','iframe');
  f.setAttribute('src','http://www.wtsp.com/news/article/199268/8/man-falls-behind-on-payments-mortgage-company-has-home-trashed');
  f.setAttribute('frameborder','0');
  document.getElementById('content').appendChild(f);
}

I'm trying to load an iframe after everything else loads. Above is开发者_运维百科 the code. In IE9, the console shows "SCRIPT65535: Unexpected call to method or property access." pointing to the line with appendChild().

Also, is it possible to load the iframe asynchronously?


As commentors have said, likely the element with id content does not exist yet. Also, you don't need to use setAttribute, just set the DOM properties directly:

function fload(){
  var f;
  var el = document.getElementById('content');

  if (el && el.appendChild) {
    f = document.createElement('iframe');
    f.id = 'iframe';
    f.src = 'http://...';
    f.frameborder = '0';
    el.appendChild(f);
  }
}

You should not call the above function until you are confident the element exists, say after the load event or similar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜