<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>location对象属性</title> <!-- host 设置或返回主机名和当前 URL 的端口号。 hash 设置或返回从井号 (#) 开始的 URL(锚)。 href 设置或返回完整的 URL。 hostname 设置或返回当前 URL 的主机名。 port 设置或返回当前 URL 的端口号。 pathname 设置或返回当前 URL 的路径部分。 search 设置或返回从问号 (?) 开始的 URL(查询部分)。 protocol 设置或返回当前 URL 的协议。 --> </head> <body> <input type="button" id="inp" value="点击"> <script> console.log(location.host);//主机名和端口号 console.log(location.hash="#inp");//锚(#)后面的url console.log(location.href);//完整url console.log(location.hostname);//主机名 console.log(location.port);//端口号 console.log(location.pathname);//url的路径部分 console.log(location.search="?id=10");//问号(?)后面的url console.log(location.protocol);//url协议 </script> </body> </html>