개발기록/Javascript
[javascript] HTML과 script파일 불러오는 방법
네이처리
2022. 9. 7. 11:51
728x90
반응형
공부하면서 정리한 내용입니다
참고한 내용은 맨 아래의 링크를 확인해주세요
HTML 불러오는 방법
1. XMLHttpRequest 으로 body내에 넣을 조각불러오기
2. 광활한 document에서 html을 불러오기 할 경우에는 getElementById로 직접 지정해줘야 적용된다.
let xhr = new XMLHttpRequest();
xhr.open("GET", "file.html", true); //옵션 :: 전송방식, 경로, 비동기사용여부
xhr.send();
xhr.onload = function(){
console.log(xhr.responseText);
}
document.body.innerHTML = xhr.responseText; // body에 넣기
// document.getElementById("아이디").innerHTML = xhr.responseText; // Id에 넣기
script 불러오는 방법
1. 스크립트 태그 추가하기
let script = document.createElement("script");
script.src = //Link;
document.head.appendChild(script);
jQuery로 HTML, script 불러오는 방법
$.ajax({
url: "file.html", // HTML 파일 가져올 경로
dataType: "html",
success: function (html){
$("#idname").html(html); // HTML 코드 붙여넣기
$.getScript({
url: "file.js", // script 가져올 경로
success: function () { // 스크립트 가져온 후에 실행할 코드
}
});
}
});
728x90
반응형