[ASP.NET][JavaScript] ASPX主頁面與iframe互相傳值

  • 9720
  • 0

摘要:[ASP.NET][JavaScript] ASPX主頁面與iframe互相傳值

今天要下班前剛好同事問到一個問題

想在A1.ASPX頁面加上一個iframe

iframe的網頁是A2.ASPX

想把A1與A2的物件互相傳值

但是又不想用到session或是URL傳值方式

因此小弟做了一個例子參考

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>A1</title>
</head>
<script type="text/javascript">
    function SendValue() {
        var v_value = document.getElementById("Text1").value;
        var ifrmae = document.getElementById('iframe');
        ifrmae.contentDocument.getElementById("Text1").value = v_value;
    }
</script>
<body>
    <form id="form1" runat="server">
    <div>
        This is A1.ASPX
        <br />
        <input id="Text1" type="text" />
        <input id="Button1" type="button" value="傳值↓" onclick="SendValue();" />
        <br />
        <iframe src="A2.ASPX" id="iframe" width="300px" height="100px"></iframe>
    </div>
    </form>
</body>
</html>

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>A2</title>
</head>
<script type="text/javascript">
    function SendValue() {
        var v_value = document.getElementById("Text1").value;
        parent.document.getElementById("Text1").value = v_value;
    }
</script>
<body>
    <form id="form1" runat="server">
    <div>
        This is A2.ASPX
        <br />
        <input id="Text1" type="text" />
        <input id="Button1" type="button" value="傳值↑" onclick="SendValue();" />
    </div>
    </form>
</body>
</html>

Source Code下載 -- Y2J傳值.zip

 






Y2J's Life:http://kimenyeh.blogspot.tw/