该例子通过 location.protocol 属性得到当前 URL 的通信协议。
假设当前页面的 URL 是:http://www.520mg.com/p-javascript_location.shtml
<script type="text/javascript"> document.write(location.protocol); </script>
运行该例子,输出:
http:
该例子通过 location.protocol 属性来设置 URL 的协议。
假设当前页面的 URL 是:http://www.520mg.com/test.shtml
<html>
<script type="text/javascript">
function setProtocol(){
location.protocol = "https:";
}
</script>
<body>
<button onclick="setProtocol()">设定访问协议</button>
</body>
</html>
运行该例子,点击 设定访问协议 按钮,触发 setProtocol() 函数,浏览器地址栏的 URL 将变为:https://www.520mg.com/test.shtml,而浏览器也将访问该 URL。