현재 url 을 알아내서 문자열 대체후 원하는 url로 이동하기
요즘 일이 너무 없어서 검도 관련 자료를 알아보고 있었는데
거의 3달만에 일 시킴
다국어 사이트가 있는데 그건도 드림위버로 만들었는지 템플릿에서 수정한면 파일들이 수정됨
이제껏 프로젝트 하면서 이런 종류의 작업은 거의 하지 않았지만
2시간정도 고민함
현재 url 을 알아내서 현재 url 의 문자열을 대체한뒤 url 이동이 중요포인트
qiita.rb
<ul>
<li class="current guide"><a href="javascript:void(0)" class="btn-lang">日本語-</a></li>
<li class="eng guide_eng"><a href="javascript:void(0)" class="btn-lang">English</a></li>
<li class="chi guide_chi"><a href="javascript:void(0)" class="btn-lang">中文</a></li>
<li class="kor guide_kor"><a href="javascript:void(0)" class="btn-lang">한국</a></li>
<li class="easy"><a href="../guide_easy/index.html">やさしい日本語</a></li>
</ul>
qiita.rb
$(function(){
function langChg() {
var langBtn = $('.btn-lang'); // 버튼에 적용할 class
var currentUrl = window.location.href; // 현재 url 을 알아낸다
var currentPath = window.location.pathname.split("/")[1]; // 첫벗째 슬러값뒤의 문자열을 알아낸다
langBtn.on('click', function(){
//japanese 현재 클릭된 부모의 guide 클래스를 가지고 있는경우
if ($(this).parent('li').hasClass('guide')) {
//현재 url을 알아내서 currentPath로 알아낸 값을 대체한다
var newPath = currentUrl.replace(currentPath,"guide");
//url 이동
location.href = newPath;
console.log(newPath);
}
//english
if ($(this).parent('li').hasClass('guide_eng')) {
var newPath = currentUrl.replace(currentPath,"guide_eng");
location.href = newPath;
}
//chiinese
if ($(this).parent('li').hasClass('guide_chi')) {
var newPath = currentUrl.replace(currentPath,"guide_chi");
location.href = newPath;
}
//korean
if ($(this).parent('li').hasClass('guide_kor')) {
var newPath = currentUrl.replace(currentPath,"guide_kor");
location.href = newPath;
}
})
}
langChg() //함수실행
})