| ๋ชฉํ
์๊ฒฝ ๋ ๊ณ ์์ด ์ธ์ ์์(ํ๋ ์ด๋ฆฌ์คํธ, ์๊ณ, ์ผ์ชฝ ์ฌ์ด๋์ ๊ณ ์์ด๋ค ๋ฑ) ๋ค์
๊ฐ sub/ ์ html ์์์ ๊ณตํต์ ์ผ๋ก ์ฐ์ผ div์ด๋ฉฐ,
js์ script ๋ํ ๋์ํด์ผํจ

| ๋ฐฉ๋ฒ
1. ๊ณตํต์ผ๋ก ๋ฃ์ div๋ฅผ ๊ฐ์ง, html ํ์ผ์ ๋ง๋ฌ (common_widget.html)
โ ๏ธ meta tag ๋นผ๊ณ div ์๋งน์ด๋ง ๊ฐ์ง๋ ํ์ผ์ด ๋ผ์ผํจ โ ๏ธ
<!-- ๊ณตํต ํค๋ -->
<header>
<div class="header_in">
<p class="title">Insomnia's Room</p>
</div>
<div class="fullpage" id="fullscreenBtn"><i class="fas fa-expand"></i></div>
</header>
<!-- ์๊ณ ์์ ฏ -->
<div class="draggable_clock">
<i class="fas far fa-grip-lines"></i>
<div class="clock_widget">
<div class="time_hms" id="hour_min_sec"></div>
<div class="time_pmam" id="pmam"></div>
</div>
</div>
.... ๊ทธ ์ธ div์์๋ค
2. ๊ณตํต์ผ๋ก ๋ฃ์ด์ง html (common_widget.html) ํ์ผ์ css์ js๋ฅผ ๋ง๋ฌ
(common.css /sub_common.js)
// ์๋๋ ์ดํด๋ฅผ ๋๊ธฐ์ํ, sub_common.js์ ์ผ๋ถ js
// |๏ฟฃ๏ฟฃ๏ฟฃ๏ฟฃ๏ฟฃ๏ฟฃ๏ฟฃ๏ฟฃ๏ฟฃ๏ฟฃ๏ฟฃ๏ฟฃ๏ฟฃ๏ฟฃ๏ฟฃ|
// drag_box 1.์๊ณ์์ ฏ
// |๏ผฟ๏ผฟ๏ผฟ๏ผฟ๏ผฟ๏ผฟ๏ผฟ๏ผฟ๏ผฟ๏ผฟ๏ผฟ๏ผฟ๏ผฟ๏ผฟ๏ผฟ|
// \ (โขโกโข) /
$(document).ready(function () {
// drag_box_inner_btn์ li ํด๋ฆญ ์ด๋ฒคํธ ์ฒ๋ฆฌ
// ์ต์ด๋ก ์๊ณ๋ widget_active_clock๊ฐ ๋ถ์ด ์์ผ๋, ํ ๋ฒ ๋๋ฅด๋ฉด ์์ ๋๋กํจ
$(".drag_box_inner_btn .box_clock").click(function () {
let img = $(this).find("img");
if (img.hasClass("widget_active_clock")) {
img.removeClass("widget_active_clock");
} else {
img.addClass("widget_active_clock");
}
});
});
$(document).ready(function () {
// .box_clock๋ฅผ ํด๋ฆญํ ๋ .widget_active_clock ํด๋์ค๋ฅผ ํ ๊ธํ์ฌ .draggable_clock์ display ์์ฑ์ ๋ณ๊ฒฝ
$(".box_clock").click(function () {
if ($(this).children('img').hasClass("widget_active_clock")) {
$(".draggable_clock").css("display", "block");
} else {
$(".draggable_clock").css("display", "none");
}
});
3. ์์์ ๋ง๋ ๊ณตํต์ผ๋ก ๋ฃ์ div๋ฅผ ๊ฐ์ง html์ ๋ฃ์ html ํ์ผ(root_page.html)์ ์ด๊ณ
์๋์ 1(๊ณตํต์์ html)๊ณผ 2(๊ณตํต์์js) ๋ฅผ ๋ฃ์ script๋ฅผ ์์ฑํจ
// root_page.html ๋ด ์ฝ๋
<body>
<!-- ๊ณตํต ์์ ฏ์ด ๋ค์ด๊ฐ ์์น -->
<div id="common_widget"></div>
</body>
// ์๋๋ root_page.html ์ ์ตํ๋จ ๋ถ๋ถ script ๋ถ๋ฌ์ค๋ ๋ถ๋ถ์
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.0/jquery-ui.min.js"></script>
<!-- jquery-ui-touch-punch -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js">
</script>
<!-- ์ด ์๋ ์ดํด๋ฅผ ๋๊ธฐ์ํ, ์๋ ๋ด๊ฐ ์ฐ๋ ์คํฌ๋ฆฝํธ ํ์ผ CDN์ด๋ฏ๋ก ์๋๋ง ์ฐธ๊ณ ๋ฐ๋ -->
<script>
// ๊ณตํต ์์ ฏ์ ๋ก๋ํ๋ ํจ์
function loadCommonWidget() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
// ๊ณตํต ์์ ฏ์ ์ฝ์
ํ ์์น์ ๋ด์ฉ์ ๋ฃ์ต๋๋ค.
document.getElementById("common_widget").innerHTML = this.responseText;
// ๊ณตํต ์์ ฏ์ด ๋ก๋๋ ํ sub_common.js ํ์ผ์ ๋์ ์ผ๋ก ๋ก๋ํฉ๋๋ค.
var script = document.createElement("script");
script.src = "../js/sub_common.js";
script.type = "text/javascript";
document.body.appendChild(script); // ์คํฌ๋ฆฝํธ๋ฅผ body์ ์ถ๊ฐ
}
};
xhttp.open("GET", "/common_widget.html", true);
// ๊ณตํต ์์๊ฐ ์๋ ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
xhttp.send();
}
// ํ์ด์ง๊ฐ ๋ก๋๋ ๋ ๊ณตํต ์์ ฏ์ ๋ก๋ํฉ๋๋ค.
window.onload = loadCommonWidget;
</script>
์๋ ์ฝ๋๋ฅผ ํตํด ๊ณตํต์์ ์์์ ๋์ํ js (2๋ฒ ํ์ผ) ๋ฅผ ๊ฐ์ ธ์ค๊ณ
script.src = "../js/sub_common.js";
script.type = "text/javascript";
์๋ ์ฝ๋๋ฅผ ํตํด ๊ณตํต์์ html์์ div (1๋ฒ ํ์ผ) ์ ๊ฐ์ ธ์ด
xhttp.open("GET", "/common_widget.html", true);
CSS๋ common.css๋ก ์์ฑํ๊ธฐ ๋๋ฌธ์ ๊ณตํต์์๋ฅผ ๋ฃ์ html ํ์ผ(root_page.html) ์๋จ์ ์ฐ๊ฒฐ ํด๋์ด
์ ์ฉ๊ฐ๋ฅ!
| ๊ฒฐ๊ณผ



์ฒ์์ ๊ณตํต์์ ฏ์ html ๊น์ง๋ ์ ๊ฐ์ ธ์๋๋ฐ
์์ ฏ์ ๊ณตํต ์คํฌ๋ฆฝํธ ๊ฐ์ ธ์ค๋๊ฑธ,
<script src="../js/sub_common.js"></script>
์ด๋ฐ์์ผ๋ก, html ํ์ผ๋ด์
๋จ์ํ ์ฐ๊ฒฐํ๋ ค๊ณ ํ์ด์ ๋ช๋ช ์ฝ๋๋ ๋์ํ๊ณ ๋ช๊ฐ๋ ๋์์ ์ํด์ ๊ณ ์ํ๋ค..
(์์ ์๋๋๊ฒ ์๋๋ผ, ๋ช๋ช์ฝ๋๋ ๋์ํด์ ๋ ํท๊ฐ๋ฆผ)
์์ ๊ธฐ์ ํ๋ฏ, โ ๏ธ๊ณตํต์์ ฏ ๋ก๋ํ๋ ํจ์๋ด์ โ ๏ธ
๊ณตํต ์คํฌ๋ฆฝํธ ๊ฐ์ ธ์ค๋ ์ฝ๋๋ฅผ ์
๋ ฅํด์ผ
์ ์์ ์ผ๋ก ๊ฐ์ ธ์ฌ ์ ์๋๊ฑฐ์๋น
script.src = "../js/sub_common.js";
script.type = "text/javascript";