Thuộc tính search của đối tượng Location trả về hoặc thiết lập một chuỗi tương ứng với các tham số truy vấn (query parameters) của một URL.
Cú pháp
Lấy giá trị thuộc tính search
location.search
Thiết lập giá trị thuộc tính search
location.search = searchString
Ví dụ
Dưới đây là ví dụ minh họa cho thuộc tính Location search:
<!DOCTYPE html> <html> <head> <title>Location search</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-search</legend> <label for="urlSelect">URL hiện tại:</label> <input type="url" size="30" id="urlSelect" value="https://www.example.com:2544/aboutUs/CTO.htm?param=value"> <input type="button" onclick="getsearch()" value="Lấy search"> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); var urlSelect = document.getElementById("urlSelect"); function getsearch(){ divDisplay.textContent = 'URL search: '+location.search; } </script> </body> </html>
Kết quả
Ví dụ này sẽ cho kết quả sau:
Trước khi nhấn nút 'Lấy giá trị search':

Sau khi nhấn nút 'Lấy giá trị search':
