JavaScript location.protocol 属性
Location 对象的 protocol 属性用于设置或取得当前 URL 的通信协议,语法如下:
location.protocol = protocol
location.protocol 实例
例子 1
该例子通过 location.protocol 属性得到当前 URL 的通信协议。
假设当前页面的 URL 是:http://www.5idev.com/p-javascript_location.shtml
<script type="text/javascript"> document.write(location.protocol); </script>
运行该例子,输出:
http:
例子 2
该例子通过 location.protocol 属性来设置 URL 的协议。
假设当前页面的 URL 是:http://www.5idev.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.5idev.com/test.shtml,而浏览器也将访问该 URL。
