html obtain the address bar information

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
</head>
<body>
   <button onclick="getInfo();">获取信息</button>
    
    <script>
        function getInfo() {
            //获取当前网址,如: http://localhost:8081/index
            var curWwwPath = window.document.location.href;
            //获取主机地址之后的目录,如: idnex
            var pathName = window.document.location.pathname;
            var pos = curWwwPath.indexOf(pathName);
            //获取主机地址,如: http://localhost:8081
            var localhostPaht = curWwwPath.substring(0, pos);
            alert(curWwwPath);
        }
    </script>
</body>
</html>

Guess you like

Origin www.cnblogs.com/lick468/p/10942096.html