HTML DOM 位置搜尋屬性


Location search 屬性返回/設定一個字串,它與 URL 的查詢引數對應。

語法

以下為語法 −

  • 返回 **search **屬性的值
location.search
  • 設定 protocol 屬性的值
location.search = searchString

示例

我們來看一個 **Location search** 屬性示例 −

 實際演示

<!DOCTYPE html>
<html>
<head>
<title>Location protocol</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-protocol</legend>
<label for="urlSelect">Current URL:</label>
<input type="url" size="30" id="urlSelect" value="https://www.example.com:2544/aboutUs/CTO.htm">
<input type="button" onclick="getprotocol()" value="Get protocol">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var urlSelect = document.getElementById("urlSelect");
   function getprotocol(){
      divDisplay.textContent = 'URL protocol: '+location.protocol;
   }
</script>
</body>
</html>

輸出

這將產生如下輸出 −

單擊 **‘獲取 search 值’** 按鈕之前 −

單擊 **‘獲取 search 值’** 按鈕之後 −

更新於: 01-Aug-2019

96 瀏覽

開啟您的職業生涯

完成課程並獲得認證

開始學習
廣告
© . All rights reserved.