HTML DOM Location href 屬性
HTML DOM Location href 屬性返回/設定對應於 URL 路徑的字串。
語法
語法如下 −
- 返回 href 屬性的值
location.href
- 設定 href 屬性的值
location.href = href
示例
我們來看看 Location href 的一個示例 −
<!DOCTYPE html> <html> <head> <title>Location href</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-href</legend> <label for="urlSelect">Current URL:</label> <input type="url" size="30" id="urlSelect" value="https://www.example.com/aboutUs"> <input type="button" onclick="gethref()" value="Get href"> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); var urlSelect = document.getElementById("urlSelect"); function gethref(){ divDisplay.textContent = 'URL Path: '+location.href; } </script> </body> </html>
輸出
將產生以下輸出 −
點選 ‘獲取 href’ 按鈕之前 −
點選 ‘獲取 href’ 按鈕之後 −
廣告