Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
Tags
- monitorjbl 엑셀
- fullcalendar js
- PostgreSQL
- MSsql
- Oracle
- 전자서명개발
- poi monitorjbl
- xssfworkbook
- jxls 엑셀
- 대용량엑셀업로드
- monitorjbl
- xlsx-streamer
- js달력
- swift 계산기
- Xcode
- ios app 개발
- Swift
- js 전자서명
- xcode swift memo
- swift 메모장 만들기
- outofmemoryerror 방지
- 전자서명기능구현
- xssreader
- 달력기능개발
- poi 대용량
- jxls
- spring annotation
- opcpackage
- mysql
- xcode12.5.1
Archives
- Today
- Total
web developer👩🏻💻
[JavaScript] .js 파일에서 contextPath 구하여 사용하기 (sessionStorage) 본문
728x90
제일 간편하고 좋은 방법은 HTML이나 JSP에서 EL이나 , jstl을 사용하여 pageContext 개체에서 request 객체의 contextPath를 가져오면 된다.
EL | ${pageContext.request.contextPath} |
JSP | <%=request.contextPath()%> |
JSTL | <c:url value=""/> |
오늘은
js파일에서 contextPath 사용하는 방법을 적어보려고 한다.
sessionStorage
tiles 영역 header.jsp에 추가 => sessionStorage에 contextPath를 저장
<script type="text/javascript" charset="utf-8">
sessionStorage.setItem("contextpath", "${pageContext.request.contextPath}");
</script>
contextPath를 사용할 js 파일을 열어 sessionStorage.getItem("contextpath")로 꺼내온다.
var ctx = getContextPath();
function getContextPath() {
return sessionStorage.getItem("contextpath");
}
[응용]
menu.js
ajax로 url을 넣을 때 sessionStorage.getItem("contextpath")로 꺼내온 contextpath 값을 넣어주면 된다.
(function() {
var ctx = getContextPath();
function getContextPath() {
return sessionStorage.getItem("contextpath");
}
$("#boardBtn").on("click",function(){
ajaxNet("GET", ctx+"/board/board_list.jsp", "", "html", function(data) {
$(".qna-content").hide();
$(".board-content").html($(data).find(".boardListDiv"));
});
});
})();
ajax.js
function ajaxNet(type, url, param, dataType ,callback) {
$.ajax({
type: type,
url: url,
data: param,
dataType : dataType,
success: function(data, textStatus, xhr) {
return callback(data);
},
error: function(xhr, status, error) {
return callback(data)
}
});
}
location.href.indexOf
https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=mk1126sj&logNo=221019411361
[JavaScript] js 파일에서 ContextPath 설정하기
contextPath 값 설정에는 여러 방법이 있다 하지만 js 파일에서 사용하려 할 경우 위 방법은 모두 무용지...
blog.naver.com
'js' 카테고리의 다른 글
[jQuery] 동적 추가된 append에 click event 처리 (0) | 2021.10.26 |
---|---|
[AJAX] 다중 fileupload / formData 이용하여 JSON 파라미터 넘기기 & 파일업로드 구현 (1) | 2021.10.22 |
[jQuery] ajax success html에서 원하는 요소 찾기 (0) | 2021.09.15 |
[jQuery] html() 와 append() 차이 (0) | 2021.09.15 |
[JavaScript] Login 아이디 기억하기 / 쿠기 (Cookie) (0) | 2021.09.08 |