有时我们要对网页做跳转,让用户打开该页面后马上或是在一定的时间内跳转到另外一个页面,下面小编分享网页自动跳转代码给大家。
动跳转代码方案一,用<meta>里直接写刷新语句:
如下语句,红色甩部分改成自己的网页地址就好了。蓝色部分为跳转时间 下面是5秒,可以改成自己需要的时间,0表示不等待。 <html> < head> < meta http-equiv="Content-Language" content="zh-CN"> < meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312"> < meta http-equiv="refresh" content="5;url=http://www.54ske.com"> < title>html网页自动跳转代码--西农大网站</title> < /head> < body> 测试:html网页自动跳转代码<br/> 这里可以写一些文字,在跳转之前可以显示给用户!<br /> </body> < /html> 自动动跳转代码方案二,用Javascript脚本来跳转
2) Javascript的实现 <script language="Javascript" type="text/Javascript">// 以下方式直接跳转window.location.href=hello.html;// 以下方式定时跳转setTimeout("Javascript:location.href=http://www.54ske.com", 5000);</script> 优点:灵活,可以结合更多的其他功能 缺点:受到不同浏览器的影响 3) 结合了倒数的Javascript实现(IE) <span id="totalSecond">5</span><script language="Javascript" type="text/Javascript">var second = totalSecond.innerText;setInterval("redirect()", 1000);function redirect(){totalSecond.innerText=--second;if(second<0) location.href=http://www.54ske.com;}</script> 优点:更人性化 缺点:firefox不支持(firefox不支持span、div等的innerText属性) 3) 结合了倒数的Javascript实现(firefox) <script language="Javascript" type="text/Javascript">var second = document.getElementById(totalSecond).textContent;setInterval("redirect()", 1000);function redirect(){document.getElementById(totalSecond).textContent = --second;if (second < 0) location.href = http://www.;}</script> 4) 解决Firefox不支持innerText的问题 <span id="totalSecond">5</span><script language="Javascript" type="text/Javascript">if(navigator.appName.indexOf("Explorer") > -1){document.getElementById(totalSecond).innerText = "my text innerText";} else{document.getElementById(totalSecond).textContent = "my text textContent";}</script> 5) 整合3)和3) <span id="totalSecond">5</span> <script language="Javascript" type="text/Javascript">var second = document.getElementById(totalSecond).textContent; if (navigator.appName.indexOf("Explorer") > -1) { second = document.getElementById(totalSecond).innerText;} else { second = document.getElementById(totalSecond).textContent;} setInterval("redirect()", 1000);function redirect() {if (second < 0) { location.href = http://www.54ske.com;} else { if (navigator.appName.indexOf("Explorer") > -1) { document.getElementById(totalSecond).innerText = second--; } else { document.getElementById(totalSecond).textContent = second--; }}}</script> |