คุณสมบัติค่า HTML DOM Input URL จะส่งกลับสตริงหากมีการกำหนดแอตทริบิวต์ค่าของ URL อินพุต ผู้ใช้ยังสามารถตั้งค่าเป็นสตริงใหม่ได้
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
- คืนค่าสตริง
inputURLObject.value
- การตั้งค่าแอตทริบิวต์ค่าเป็นค่าสตริง
inputURLObject.value = ‘String’
ตัวอย่าง
ให้เราดูตัวอย่างสำหรับ ค่า URL ที่ป้อน ทรัพย์สิน −
<!DOCTYPE html>
<html>
<head>
<title>Input URL value</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>URL-value</legend>
<label for="URLSelect">URL Id:
<input type="url" id="URLSelect">
<input type="button" onclick="getUserURL('google')" value="Google">
<input type="button" onclick="getUserURL('bing')" value="Bing"><br>
<input type="button" onclick="redirection()" value="Go">
</label>
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var inputURL = document.getElementById("URLSelect");
function getUserURL(userName) {
if(userName === 'google')
inputURL.value = 'https://www.google.com';
else
inputURL.value = 'https://www.bing.com';
}
function redirection() {
if(inputURL.value !== '')
divDisplay.textContent = 'Redirecting to '+inputURL.value;
else
divDisplay.textContent = 'Enter URL';
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
คลิก 'ไป' ปุ่มที่มีช่อง URL ว่าง -

หลังจากคลิก ‘Bing’ ปุ่ม −

หลังจากคลิก 'ไป' ปุ่มพร้อมชุดฟิลด์ url -
