ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ณต๋ถ€/JavaScript

[ex18~22] screen, location, history, collection, link

๋Œ€์žฅ์ฟต์•ผ 2023. 4. 25. 09:02

ex18

 

screen ๊ฐ์ฒด
 - window ๊ฐ์ฒด์˜ ์ž์‹
 - ํ™”๋ฉด ์ •๋ณด ์ œ๊ณต

//ํ˜„์žฌ ๋ณด๊ณ  ์žˆ๋Š” ํ™”๋ฉด์˜ ํฌ๊ธฐ > ๋ชจ๋‹ˆํ„ฐ ํ•ด์ƒ๋„ (๋ฐฐ์œจ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ด)
console.log(window.screen.availWidth); //1536
console.log(window.screen.availHeight); //824

console.log(window.screen.colorDepth); //24bit color
console.log(window.screen.orientation); //ScreenOrientation {anger: 0, type: 'landscape-primary', onchange: null}

ex19

 

location ๊ฐ์ฒด

- window์˜ ์ž์‹

- ํ˜„์žฌ ์ฐฝ์˜ ํŽ˜์ด์ง€(URL)์™€ ๊ด€๋ จ๋œ ์กฐ์ž‘

<body>
    <!-- ex19_location.html -->

    <form name="form1">
        <input type="button" name="btn1" value="ํ™•์ธ">
    </form>

    <script>

        window.document.form1.btn1.onclick = m1;

        function m1() {

            //href > reference a hyperlink
            // console.log(window.location.href);

            //JavaScript + ํŽ˜์ด์ง€ ์ด๋™
            // window.location.href = 'https://naver.com';

            // console.log(window.location.host); //127.0.0.1:5500
            // console.log(window.location.hostname); //127.0.0.1
            // console.log(window.location.protocol); //http:
            // console.log(window.location.port); //5500

            //window.location.reload(); //์ƒˆ๋กœ ๊ณ ์นจ(F5)
            // window.location.href = 'ex19_location.html'; //์ƒˆ๋กœ ๊ณ ์นจ

            // window.location.replace('https://naver.com');
            window.location.href = 'https://naver.com';


            //window.location.href > ***์ œ์ผ ๋งŽ์ด ์”€
            //window.location.reload()
            //window.location.replace()
        }
    </script>
</body>
</html>

ex20

history ๊ฐ์ฒด

- window์˜ ์ž์‹

- ๋ธŒ๋ผ์šฐ์ €์˜ ํƒ์ƒ‰ ๊ธฐ๋ก(history)  ์˜์—ญ ์ ‘๊ทผํ•˜๋Š” ๊ฐ์ฒด

<body>
    <!-- ex20_history.html -->

    <form name="form1">
        <input type="button" name="btn1" value="๋’ค๋กœ ๊ฐ€๊ธฐ">
        <input type="button" name="btn2" value="์•ž์œผ๋กœ ๊ฐ€๊ธฐ">
        <input type="button" name="btn3" value="2๋‹จ๊ณ„ ๋’ค๋กœ ๊ฐ€๊ธฐ">
        <input type="button" name="btn4" value="2๋‹จ๊ณ„ ์•ž์œผ๋กœ ๊ฐ€๊ธฐ">
    </form>

    <script>

        //history ๊ฐ์ฒด
        //- window์˜ ์ž์‹
        //- ๋ธŒ๋ผ์šฐ์ €์˜ ํƒ์ƒ‰ ๊ธฐ๋ก(history) ์˜์—ญ ์ ‘๊ทผํ•˜๋Š” ๊ฐ์ฒด

        // window.document.form1.btn1 > window(๋งŒ) ์ƒ๋žต ๊ฐ€๋Šฅ

        document.form1.btn1.onclick = m1;
        document.form1.btn2.onclick = m2;
        document.form1.btn3.onclick = m3;
        document.form1.btn4.onclick = m4;

        //ex20 > ๋„ค์ด๋ฒ„ > ๋‹ค์Œ > ex20 > ๊ตฌ๊ธ€ > yes24
        function m1() {
            window.history.back(); //๋’ค๋กœ ๊ฐ€๊ธฐ
        }

        function m2() {
            history.forward(); //์•ž์œผ๋กœ ๊ฐ€๊ธฐ
        }

        function m3() {
            history.go(-2);
        }

        function m4() {
            history.go(2);
        }

    </script>
</body>

ex21

 

๋‚ด์žฅ ๋ฐฐ์—ด(์ปฌ๋ ‰์…˜), collection
- ๋ฌธ์„œ ์ฝ”๋“œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž๋™ ์ƒ์„ฑ๋˜๋Š” ๋ฐฐ์—ด์„ ์ œ๊ณตํ•œ๋‹ค.

1. window.document.images > ๋ฌธ์„œ๋‚ด์˜ ๋ชจ๋“  <img> ํƒœ๊ทธ
2. window.document.links > ๋ฌธ์„œ๋‚ด์˜ ๋ชจ๋“  <a href=""> ํƒœ๊ทธ
3. window.document.anchors > ๋ฌธ์„œ๋‚ด์˜ ๋ชจ๋“  <a name=""> ํƒœ๊ทธ
4. window.document.forms > ๋ฌธ์„œ๋‚ด์˜ ๋ชจ๋“  <form> ํƒœ๊ทธ
5. window.document.forms[index].elements > ํŠน์ • ํผํƒœ๊ทธ ๋‚ด์˜ ๋ชจ๋“  ์ž…๋ ฅํƒœ๊ทธ
6. window.document.forms[index].select.option > <option> ํƒœ๊ทธ
7. window.document.all > ๋ฌธ์„œ์ƒ์˜ ๋ชจ๋“  ํƒœ๊ทธ(๋น„ํ‘œ์ค€ MS)

 

๋”๋ณด๊ธฐ
<body>
    <!-- ex21_collection.html -->
    <h1>์ด๋ฏธ์ง€</h1>

    <img src="images/catty01.png">
    <img src="images/catty02.png">
    <img src="images/catty03.png" name="cat3">
    <img src="images/catty04.png">
    <img src="images/catty05.png">

    <h1>๋งํฌ+์•ต์ปค</h1>
    <a href="#1" name="link1">๋งํฌ1</a>
    <a href="#2">๋งํฌ2</a>
    <a href="#3">๋งํฌ3</a>
    <a name="a1">์•ต์ปค1</a>
    <a name="a2">์•ต์ปค2</a>

    <h1>ํผ</h1>
    <form name="form1">
        <input type="text" name="txt1">
        <input type="text" name="txt2">
        <input type="text" name="txt3">
    </form>
    
    <form>
        
    </form>

    <script>

        // console.log(document.images.length);

        // for (var i=0; i<5; i++) {
        //     console.log(document.images[i].src);
        //     //http://127.0.0.1:5500/javascript/images/catty01.png
        // }

        // document.images[0].width = 300;
        // document.images['cat3'].width = 300;

        // console.log(document.links.length);
        // console.log(document.links[0].href);
        // console.log(document.links['link1'].href);

        // console.log(document.anchors.length);
        // console.log(document.anchors[0].name);
        // console.log(document.anchors['a1'].name);

        // document.form1
        // document.forms[0]
        // console.log(document.forms.length);

        //form1ํƒœ๊ทธ์˜ ์ž์‹ํƒœ๊ทธ
        // console.log(document.form1.elements.length);
        // document.form1.txt1.value = "ํ™๊ธธ๋™";
        // document.form1.elements[1].value = '์•„๋ฌด๊ฐœ';

        //all > ๋ฌธ์„œ์ƒ์˜ ๋ชจ๋“  <form> + ์ž…๋ ฅ ํƒœ๊ทธ + ์ด๋ฏธ์ง€ + ๋งํฌ
        //all > ๋ฌธ์„œ์ƒ์˜ ๋ชจ๋“  ํƒœ๊ทธ
        console.log(document.all.length); //26

    </script>
</body>
</html>

ex22

 

Link

๋”๋ณด๊ธฐ
<body>
    <!-- ex22_link.html -->

    <h1>๋งํฌ</h1>

    <a href="https://naver.com" name="link1">๋„ค์ด๋ฒ„</a>
    <hr>
    <button type="button" name="btn1">ํ™•์ธ</button>

    <script>

        document.all.btn1.onclick = m1;

        function m1() {

            // link1์˜ ์†์„ฑ๊ฐ’์„ ๋„ค์ด๋ฒ„ > ๊ตฌ๊ธ€๋กœ ์กฐ์ž‘
            document.links['link1'].href = 'http://google.com';
            // ์ƒˆ์ฐฝ์—์„œ ์—ด๋ฆผ
            document.links['link1'].target = '_blank';
            document.links['link1'].title = '๊ตฌ๊ธ€๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค';

            //DOM
            // document.links['link1'].innerText = '๊ตฌ๊ธ€'; //๋น„ํ‘œ์ค€(MS)
            document.links['link1'].textContent = '๊ตฌ๊ธ€'; //ํ‘œ์ค€

        }

    </script>

</body>
</html>