Thuộc tính origin của đối tượng Location trả về chuỗi tương ứng với giao thức (protocol), tên máy chủ (hostname) và cổng (port) của URL (nếu được chỉ định).
Cú pháp
Lấy giá trị thuộc tính origin:
location.origin
Ví dụ
Dưới đây là ví dụ minh họa cho thuộc tính Location origin:
<!DOCTYPE html> <html> <head> <title>Location origin</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-origin</legend> <label for="urlSelect">URL hiện tại:</label> <input type="url" size="30" id="urlSelect" value="https://www.example.com:2544/aboutUs"> <input type="button" onclick="getorigin()" value="Lấy origin"> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); var urlSelect = document.getElementById("urlSelect"); function getorigin(){ divDisplay.textContent = 'URL Origin: '+location.origin; } </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 origin':

Sau khi nhấn nút 'Lấy origin':
