문제 상황
원인 추론
해결 방법
Location (현재 URL) Property 중에서 query string이라고 불리는 search string을 사용하여 해결
search string은 url에서 ?
를 포함한 뒷부분을 string 형식으로 반환해주는 역할
실제 적용 코드)
followingList.html에서 유저를 선택할 때 넘어가는 a태그의 href 주소를 ?accountname=유저의 accountname
붙여서 보냄
link.setAttribute(
'href',
'../pages/myProfile.html?accountname=' + resJson[i].accountname
);
프로필 html로 넘어와서 상대방의 accountname을 location.search로 변수에 담아 사용
내 계정일 경우에는 로컬스토리지에서, 상대방 프로필이면 url을 query string값이 accountname에 들어가게 처리
location.search⇒ ?accountname=유저아이디
location.search.replace(’?’,’’) ⇒ accountname=유저아이디
location.search.replace(’?’,’’).split(’=’) ⇒ ['accountname', '유저아이디']
📌 userInfo.js (프로필 상단, 유저 정보 API GET 요청 코드)
const url = 'https://mandarin.api.weniv.co.kr';
const myAccountname = `${window.localStorage.getItem('accountname')}`;
const yourAccountname = location.search.replace('?', '').split('=')[1];
const accountname = yourAccountname ? yourAccountname : myAccountname;
const userSettings = document.querySelector('.userSettings');
//프로필 정보 보여주기
async function infoUser() {
try {
const res = await fetch(url + '/profile/' + accountname, {
method: 'GET',
..생략..}
}
마이프로피로/유어프로필과 팔로잉/팔로워가 서로 같은 html 파일을 공유하고 있어, 404 에러를 많이 겪게 됨
Js에서 흔하게 사용하는 뒤로가기인 window.history.back()
또는 window.history.go(-1)
만 썼을 때 에러 발생
Location Porperty를 사용해서 예외처리로 Error 해결
const btnBack = document.querySelector('.btnBack');
btnBack.addEventListener('click', () => {
location.href = window.history.back();
});
const locationPath = location.pathname.split('.')[0].split('/')[3];
const btnBack = document.querySelector('.btnBack');
btnBack.addEventListener('click', () => {
if (accountname == yourAccountname) {
location.href = `../pages/${locationPath}.html?accountname=${yourAccountname}`;
} else if (accountname == myAccountname) {
location.href = `../pages/${locationPath}.html`;
}else {
location.href = window.history.back();
}
});
문제 상황
해결 방법
기존 코드)
function createProductList() {
productList.innerHTML = prodcutListDummy
.map(
(element) =>
`
<li>
<button type="button" class="btnProductItem" id="btnProductItem">
<img src=${element.itemImage} alt="상품1" />
<span class="prodcutTitle">${element.itemName}</span>
<strong class="prodcutPrice">${element.price
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}</strong>
</button>
</li>
`
)
.join('');
}
해결 코드)
function createProductList() {
for (let i = 0; i < prodcutListDummy.length; i++) {
const li = document.createElement('li');
const button = document.createElement('button');
const img = document.createElement('img');
const span = document.createElement('span');
const strong = document.createElement('strong');
**생략 ...**
}
문제 상황
원인 추론
문제의 코드
const btnProductItem = document.qeurySelectorAll('.btnProductItem')
function onProductSettingModal() {
btnProductItem.addEventListenr('click', () => {
...**생략...**
})
}
function onProductSettingModal() {
let btnProductItem = document.querySelectorAll('.btnProductItem');
for (let i = 0; i < btnProductItem.length; i++) {
btnProductItem[i].addEventListener('click', (event) => {
...**생략...**
}