2023년 신상 호텔 리스트
최근에 오픈한 호텔을 찾는다면 살펴보세요

JavaScript - 자바스크립트 includes 메서드 & replace 메서드

조회수: 760

includes() 메서드: 해당하는 value가 있는지 체크.

replace() 메서드: 새로운 value로 변경.

 

최근에 위 두 메소드를 간단하게 이용해보았다.

'&'가 포함이 된 제목이 있었다. 그래서 url에서는 제대로 작동이 되지 않는다.

즉 '&'를 '%26'으로 변경하게 했다.

 

<!DOCTYPE html>
<html>
<head>
<title>상식닷컴</title>
</head>
<body>

<h1>상식닷컴</h1>
<p>재밌게 천천히 배우자!</p>

<p><a id="redirect_link" href="https://naver.com">이동하기</a></p>


<script>	
	var place = "방콕왕궁 & 왓 포";

	if(place.includes("&")) {
	    var place = place.replace("&", "%26");
	    document.getElementById('redirect_link').href = `https://search.naver.com/search.naver?query=${place}&where=blog`;      
	}	 
</script>

</body>
</html>