Language/JavaScript (WEB)

๐ŸŒ ๋ถ€๋ชจ์ฐฝ ↔ ์ž์‹์ฐฝ ์„œ๋กœ ๊ฐ’ ์ „๋‹ฌํ•˜๊ธฐ

์ธํŒŒ_ 2021. 9. 27. 06:17

window.open

๋ถ€๋ชจ์ฐฝ์—์„œ ์ž์‹์ฐฝ์œผ๋กœ ๊ฐ’ ์ „๋‹ฌํ•˜๊ธฐ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ โ€‹window.open()์„ ์ด์šฉํ•˜๋ฉด ํŒ์—…์ฐฝ์„ ๋„์šธ ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ํŒ์—…์ฐฝ์€ ์ž์‹์ฐฝ, ํŒ์—…์„ ๋„์šฐ๋Š” ์ฐฝ์€ ๋ถ€๋ชจ์ฐฝ์ด ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ž์‹์ฐฝ๊ณผ ๋ถ€๋ชจ์ฐฝ๊ฐ„์—๋Š” ์„œ๋กœ ๊ฐ’์„ ์ฃผ๊ณ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

๋ถ€๋ชจ์ฐฝ์—์„œ ์ž์‹์ฐฝ์— ์ ‘๊ทผํ•˜๋ ค๋ฉด, ์šฐ์„  window.open()์„ ํ†ตํ•ด ์–ป์€ ํŒ์—…์ฐฝ ๊ฐ์ฒด๋ฅผ ๋ณ€์ˆ˜์— ๋‹ด๊ณ  ๊ทธ ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•˜๋ฉด ๋œ๋‹ค. 

let openWin = window.open("Child.html");
openWin.document.getElementById("cInput").value = "์ „๋‹ฌํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ’";

โ€‹

Parent.html

<!DOCTYPE html>
<html>
<head>
    <title>Parent</title>
</head>
<body>
    <br>
    <b><font size="5" color="gray">๋ถ€๋ชจ์ฐฝ</font></b>
    <br><br>
    
    <input type="button" value="์ž์‹์ฐฝ ์—ด๊ธฐ" onclick="openChild()">
    <br>์ „๋‹ฌํ•  ๊ฐ’ : <input type="text" id="pInput"> 
    <input type="button" value="์ „๋‹ฌ" onclick="setChildText()">
    
    <script type="text/javascript">
        let openWin;

        function openChild() {
            // window.name = "๋ถ€๋ชจ์ฐฝ ์ด๋ฆ„"; 
            window.name = "parentForm";
            
            // window.open("openํ•  window", "์ž์‹์ฐฝ ์ด๋ฆ„", "ํŒ์—…์ฐฝ ์˜ต์…˜");
            openWin = window.open("Child.html", "childForm", "width=570, height=350, resizable = no, scrollbars = no");    
        }

        function setChildText(){
            openWin.document.getElementById("cInput").value = document.getElementById("pInput").value;
        }
    </script>
</body>
</html>

window.open

[์ž์‹์ฐฝ ์—ด๊ธฐ]๋ฅผ ํด๋ฆญํ•˜๋ฉด openChild() ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋ฉฐ ์ž์‹์ฐฝ์ด ์—ด๋ฆฐ๋‹ค. ๊ทธ๋ฆฌ๊ณ  [์ „๋‹ฌ] ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด setChildText()๊ฐ€ ์‹คํ–‰๋˜๋ฉฐ, ํŒ์—…์ฐฝ์˜ dom์œผ๋กœ ์ ‘๊ทผํ• ์ˆ˜ ์žˆ๊ฒŒ ๋˜์–ด ์ž์‹์ฐฝ์œผ๋กœ ๊ฐ’์ด ์ „๋‹ฌ๋˜๊ฒŒ ๋œ๋‹ค.

โ€‹

Child.html

<!DOCTYPE html>
<html>
<head>
    <title>Child</title>
</head>
<body>
    <br>
    <b><font size="5" color="gray">์ž์‹์ฐฝ</font></b>
    <br><br>
 
    <input type="text" id="cInput"><br><br>
    <input type="button" value="์ฐฝ๋‹ซ๊ธฐ" onclick="window.close()">
</body>
</html>

window.open


์ž์‹์ฐฝ์—์„œ ๋ถ€๋ชจ์ฐฝ์œผ๋กœ ๊ฐ’ ์ „๋‹ฌํ•˜๊ธฐ

๊ฑฐ๊พธ๋กœ ์ž์‹์ฐฝ์—์„œ ๋ถ€๋ชจ์ฐฝ์œผ๋กœ ๊ฐ’์„ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์€ ์œ„์˜ ๊ณผ์ •์„ ๋ฐ˜๋Œ€๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋œ๋‹ค.

์ด๋•Œ ์ž์‹์ฐฝ์—์„œ ๋ถ€๋ชจ์ฐฝ์— ์ ‘๊ทผํ•˜๋ ค๋ฉด window.opener ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด์„œ ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค. (window ๋Š” ์ƒ๋žต ๊ฐ€๋Šฅ)

opener ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด ๋ถ€๋ชจ์ฐฝ์˜ DOM์— ์ ‘๊ทผํ•˜์—ฌ ํŠน์ • element์— ๊ฐ’์„ ๋‹ด์œผ๋ฉด ๋œ๋‹ค.

opener.document.getElementById("pInput").value = "์ „๋‹ฌํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ’";

 

Child.html

<!DOCTYPE html>
<html>
<head>
    <title>Child</title>
</head>
<body>
    <br>
    <b><font size="5" color="gray">์ž์‹์ฐฝ</font></b>
    <br><br>
 
    <input type="text" id="cInput"> <input type="button" value="์ „๋‹ฌ" onclick="setParentText()">
    <br><br>
    <input type="button" value="์ฐฝ๋‹ซ๊ธฐ" onclick="window.close()">
    
    <script>
        function setParentText(){
        	opener.document.getElementById("pInput").value = document.getElementById("cInput").value
        }
    </script>
</body>
</html>

window.open


์ž์‹์ฐฝ์—์„œ ๋ถ€๋ชจ์ฐฝ์˜ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ

window.opener ๊ฐ์ฒด๋ฅผ ๋”์šฑ ์‘์šฉํ•ด์„œ ์ด๋ฒˆ์—” ๊ฐ€์ ธ์˜ค๋Š” ๋™์ž‘๋„ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์œ„์˜ ์ „๋‹ฌ ๊ธฐ๋Šฅ์„ ์ดํ•ดํ•˜์˜€์œผ๋ฉด ๊ฐ€์ ธ์˜ค๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ ์–ด๋ ต์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค.

const value = opener.document.getElementById("pInput").value;

 

Child.html

<!DOCTYPE html>
<html>
<head>
    <title>Child</title>
</head>
<body>
    <br>
    <b><font size="5" color="gray">์ž์‹์ฐฝ</font></b>
    <br><br>
 
    <input type="text" id="cInput"> <input type="button" value="๊ฐ€์ ธ์˜ค๊ธฐ" onclick="setParentText()">
    <br><br>
    <input type="button" value="์ฐฝ๋‹ซ๊ธฐ" onclick="window.close()">
    
    <script>
        function setParentText(){
        	document.getElementById("cInput").value = opener.document.getElementById("pInput").value;
        }
    </script>
</body>
</html>

window.open


๋ถ€๋ชจ์ฐฝ์—์„œ ์ž์‹์ฐฝ์˜ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ

ํฌ์ŠคํŒ… ์ดˆ๋ฐ˜์˜ ๋ถ€๋ชจ์ฐฝ์—์„œ ์ž์‹์ฐฝ์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋’ค์ง‘๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

const value = openWin.document.getElementById("cInput").value;

 

Parent.html

<!DOCTYPE html>
<html>
<head>
    <title>Parent</title>
</head>
<body>
    <br>
    <b><font size="5" color="gray">๋ถ€๋ชจ์ฐฝ</font></b>
    <br><br>
    
    <input type="button" value="์ž์‹์ฐฝ ์—ด๊ธฐ" onclick="openChild()">
    <br>์ „๋‹ฌํ•  ๊ฐ’ : <input type="text" id="pInput"> 
    <input type="button" value="์ „๋‹ฌ" onclick="setChildText()">
    <input type="button" value="๊ฐ€์ ธ์˜ค๊ธฐ" onclick="getChildText()">
    
    <script type="text/javascript">
        let openWin;

        function openChild() {
            // window.name = "๋ถ€๋ชจ์ฐฝ ์ด๋ฆ„"; 
            window.name = "parentForm";
            
            // window.open("openํ•  window", "์ž์‹์ฐฝ ์ด๋ฆ„", "ํŒ์—…์ฐฝ ์˜ต์…˜");
            openWin = window.open("Child.html", "childForm", "width=570, height=350, resizable = no, scrollbars = no");    
        }

        function setChildText(){
            openWin.document.getElementById("cInput").value = document.getElementById("pInput").value;
        }

        function getChildText(){
            document.getElementById("pInput").value = openWin.document.getElementById("cInput").value;
        }
    </script>
</body>
</html>

window.open