假设有一种需求,打开xxx.html文件时,会自动跳转到一个指定的地址。在这个地址上的文件,去做具体的业务。
怎么实现这种跳转。在网上找到了如下几种方法:
方法1、使用meta refresh实现,示例如下:
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="3;url=www.baidu.com ">
<title>Document</title>
</head>
上面语句中的“3”是指,延迟多少时间跳转,如果是“0”,会立即跳转。
注意,url中要加上“https://”头,否则会用默认的头“file://”导致跳转失败。
如果跳转的文件就在本地,可以不加“https://”
方法2、使用window.location.href实现,示例如下:
<body>
<script>
window.location.href="https://www.baidu.com"
</script>
</body>
方法3、使用 body onload实现,示例如下:
<body onload="parent.location='https://www.baidu.com'">
</body>
方法4、使用表单实现,示例如下:
<body>
<form name="form1" action="https://www.baidu.com" method="get"></form>
<script>
document.form1.submit();
</script>
</body>
方法5、使用javascript实现,示例如下:
<body>
<script>
location.replace("https://www.baidu.com");
</script>
</body>