HTML DOM Location replace() 메서드는 현재 문서를 대체하는 새 문서를 렌더링하는 데 사용됩니다. 또한 '뒤로'를 사용하여 이전 문서로 이동할 수 없도록 문서 기록에서 현재 문서 URL을 제거합니다. 버튼.
구문
다음은 구문입니다 -
location.replace(URLString)
예시
위치 바꾸기()의 예를 살펴보겠습니다. 속성 -
<!DOCTYPE html>
<html>
<head>
<title>Location replace()</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
}
input[type="button"] {
border-radius: 10px;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>Location-replace( )</legend>
<label for="urlSelect">Current URL: </label>
<input type="url" id="urlSelect" value="https://www.example.com"><br>
<label for="newUrlSelect">New URL: </label>
<input type="url" id="newUrlSelect" placeholder="Give new url..."><br>
<input type="button" onclick="doReplace()" value="Go to new URL">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var urlSelect = document.getElementById("urlSelect");
var newUrlSelect = document.getElementById("newUrlSelect");
function doReplace(){
if(newUrlSelect.value === '')
divDisplay.textContent = 'Provide new URL';
else{
location.replace(newUrlSelect.value);
divDisplay.textContent = 'Redirecting to new URL';
}
}
</script>
</body>
</html> 출력
이것은 다음과 같은 출력을 생성합니다 -
'새 URL로 이동'을 클릭한 후 입력이 없는 버튼 -

'새 URL로 이동' 클릭 세부 정보를 채운 후 버튼 -
