javascript子窗口向父窗口传值实例



javascript子窗口向父窗口传值实例。

方案1:

parent01.html

Java代码 复制代码 收藏代码
  1. <!doctype html public ”-//w3c//dtd html 4.0 transitional//en”>   
  2. <html>   
  3. <head>   
  4.     <title>父窗口</title>   
  5.     <meta name=”generator” content=”editplus”>   
  6.     <meta name=”author” content=”">   
  7.     <meta name=”keywords” content=”">   
  8.     <meta name=”description” content=”">   
  9. </head>   
  10. <script type=”text/javascript”>   
  11.     function addwindow() {   
  12.         var href = ”/demo/window/son01.html”;   
  13.         window.open(href, ”newwindow”, ”height=200,width=400,toolbar=no,menubar=no,scrollbars=no,resizable=no, location=no,status=no”);   
  14.     }   
  15.     function add(id, textvalue) {   
  16.         document.getElementById(id).value = textvalue;   
  17.     }   
  18. </script>   
  19. <body>   
  20. <input id=”newId” type=”text”><br>   
  21. <input type=”button” value=”addwindow” onclick=”addwindow()”>   
  22. </body>   
  23. </html>  
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
    <title>父窗口</title>
    <meta name="generator" content="editplus">
    <meta name="author" content="">
    <meta name="keywords" content="">
    <meta name="description" content="">
</head>
<script type="text/javascript">
    function addwindow() {
        var href = "/demo/window/son01.html";
        window.open(href, "newwindow", "height=200,width=400,toolbar=no,menubar=no,scrollbars=no,resizable=no, location=no,status=no");
    }
    function add(id, textvalue) {
        document.getElementById(id).value = textvalue;
    }
</script>
<body>
<input id="newId" type="text"><br>
<input type="button" value="addwindow" onclick="addwindow()">
</body>
</html>

son01.html

Java代码 复制代码 收藏代码
  1. <!DOCTYPE HTML PUBLIC ”-//W3C//DTD HTML 4.0 Transitional//EN”>   
  2. <HTML>   
  3. <HEAD>   
  4.     <TITLE>子窗口</TITLE>   
  5.     <META NAME=”Generator” CONTENT=”EditPlus”>   
  6.     <META NAME=”Author” CONTENT=”">   
  7.     <META NAME=”Keywords” CONTENT=”">   
  8.     <META NAME=”Description” CONTENT=”">   
  9. </HEAD>   
  10.   
  11. <script type=”text/javascript”>   
  12.     function test() {   
  13.         var textValue;   
  14.         for (var i = 0; i < document.getElementById(“newId”).length; i++) {   
  15.             if (document.getElementById(“newId”).options[i].selected) {   
  16.                 textValue = document.getElementById(“newId”).options[i].text;   
  17.                 break;   
  18.             }   
  19.         }   
  20.         window.opener.add(“newId”, textValue);   
  21.         self.close();   
  22.     }   
  23. </script>   
  24.   
  25. <BODY>   
  26. <form id=”form1″>   
  27.     <select id=”newId”>   
  28.         <option value=”1″>1</option>   
  29.         <option value=”2″>2</option>   
  30.         <option value=”3″>3</option>   
  31.     </select>   
  32.     <input type=”button” value=”submit” onclick=”test()”>   
  33. </form>   
  34. </BODY>   
  35. </HTML>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
    <TITLE>子窗口</TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
</HEAD>

<script type="text/javascript">
    function test() {
        var textValue;
        for (var i = 0; i < document.getElementById("newId").length; i++) {
            if (document.getElementById("newId").options[i].selected) {
                textValue = document.getElementById("newId").options[i].text;
                break;
            }
        }
        window.opener.add("newId", textValue);
        self.close();
    }
</script>

<BODY>
<form id="form1">
    <select id="newId">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <input type="button" value="submit" onclick="test()">
</form>
</BODY>
</HTML>

方案2


parent02.html

Java代码 复制代码 收藏代码
  1. <!doctype html public ”-//w3c//dtd html 4.0 transitional//en”>   
  2. <html>   
  3. <head>   
  4.     <title>父窗口</title>   
  5.     <meta name=”generator” content=”editplus”>   
  6.     <meta name=”author” content=”">   
  7.     <meta name=”keywords” content=”">   
  8.     <meta name=”description” content=”">   
  9.     <script type=”text/javascript” src=”/js/jquery/jquery-1.8.1.min.js”></script>   
  10.     <script language=”javascript” src=”/js/com.js”></script>   
  11. </head>   
  12. <script type=”text/javascript”>   
  13.     $(document).ready(function () {   
  14.         $(“#addwindow”).click(function(e) {   
  15.             var href = ”/demo/window/son02.html”;   
  16.             var textValue = openDialog(href,750, 350, ”yes”, e);   
  17.             if (textValue != ”" && textValue != null) {   
  18.                 add(“newId”,textValue);   
  19.             }   
  20.         });   
  21.     });   
  22.     function add(id, textvalue) {   
  23.         document.getElementById(id).value = textvalue;   
  24.     }   
  25. </script>   
  26. <body>   
  27. <input id=”newId” type=”text”><br>   
  28. <input type=”button” id=”addwindow” value=”addwindow”>   
  29. </body>   
  30. </html>  
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
    <title>父窗口</title>
    <meta name="generator" content="editplus">
    <meta name="author" content="">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <script type="text/javascript" src="/js/jquery/jquery-1.8.1.min.js"></script>
    <script language="javascript" src="/js/com.js"></script>
</head>
<script type="text/javascript">
    $(document).ready(function () {
        $("#addwindow").click(function(e) {
            var href = "/demo/window/son02.html";
            var textValue = openDialog(href,750, 350, "yes", e);
            if (textValue != "" && textValue != null) {
                add("newId",textValue);
            }
        });
    });
    function add(id, textvalue) {
        document.getElementById(id).value = textvalue;
    }
</script>
<body>
<input id="newId" type="text"><br>
<input type="button" id="addwindow" value="addwindow">
</body>
</html>

son02.html

Java代码 复制代码 收藏代码
  1. <!DOCTYPE HTML PUBLIC ”-//W3C//DTD HTML 4.0 Transitional//EN”>   
  2. <HTML>   
  3. <HEAD>   
  4.     <TITLE>子窗口</TITLE>   
  5.     <META NAME=”Generator” CONTENT=”EditPlus”>   
  6.     <META NAME=”Author” CONTENT=”">   
  7.     <META NAME=”Keywords” CONTENT=”">   
  8.     <META NAME=”Description” CONTENT=”">   
  9. </HEAD>   
  10.   
  11. <script type=”text/javascript”>   
  12.     function test() {   
  13.         var textValue;   
  14.         for (var i = 0; i < document.getElementById(“newId”).length; i++) {   
  15.             if (document.getElementById(“newId”).options[i].selected) {   
  16.                 textValue = document.getElementById(“newId”).options[i].text;   
  17.                 break;   
  18.             }   
  19.         }   
  20.         window.returnValue = textValue;   
  21.         window.close();   
  22.     }   
  23. </script>   
  24.   
  25. <BODY>   
  26. <form id=”form1″>   
  27.     <select id=”newId”>   
  28.         <option value=”1″>1</option>   
  29.         <option value=”2″>2</option>   
  30.         <option value=”3″>3</option>   
  31.     </select>   
  32.     <input type=”button” value=”submit” onclick=”test()”>   
  33. </form>   
  34. </BODY>   
  35. </HTML>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
    <TITLE>子窗口</TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
</HEAD>

<script type="text/javascript">
    function test() {
        var textValue;
        for (var i = 0; i < document.getElementById("newId").length; i++) {
            if (document.getElementById("newId").options[i].selected) {
                textValue = document.getElementById("newId").options[i].text;
                break;
            }
        }
        window.returnValue = textValue;
        window.close();
    }
</script>

<BODY>
<form id="form1">
    <select id="newId">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <input type="button" value="submit" onclick="test()">
</form>
</BODY>
</HTML>

  • 大小: 11.1 KB
  • 大小: 20.9 KB