---
title: "JavaScript跨域问题"
author: "cjx186"
date: "2009-11-08T23:31:52Z"
tags: []
slug: "javascript"
draft: false
lastmod: "2009-11-08T23:45:57Z"
id: 512587
url: "https://xml.iteye.com/blog/512587"
---

JavaScript用得越来越多了，有的在head里添加<Script>然后指定src，其实也可以用IFrame实现。

```js

    //mainBody是IFrame的ID
    var objFrm = document.getElementById('mainBody');
    //source.html是被载入页面
    objFrm.src="source.html";
    //FF(onload)和IE(onreadystatechange )不用方式
    objFrm.onload = objFrm.onreadystatechange = function() {
        //每当iframe加载页面，过程内会激活onreadystatechange事件三次，相应的状态分别是loading,interactive和complete，而最后一次才是complete.
        if (this.readyState && this.readyState != 'complete')
            return;
        else {
            eval(document.getElementById("mainBody").contentWindow.document.body.innerHTML);//拿到文本想干啥干啥
        }//end if
    }//end fun

```