Javascript实例教程(8)

利用Javascript基于浏览器类型的重定向

基于浏览器类型的重定向的实现可以通过使用JavaScript函数来检查navigator.userAgent的字符串“MSIE”,它将告诉你用户是否使用Microsoft Internet Explorer(微软的IE浏览器)。通过修改windows.location函数可以重定向到正确的URL(同意资源定位器)。下面是详细的代码:

 1<html>
 2<head>
 3<script language="JavaScript">   
 4  
 5<!--   
 6  
 7function redirectClient(ieurl, nsurl) {   
 8  
 9// test for Internet Explorer (any version)   
10  
11if (navigator.userAgent.indexOf("MSIE") != -1) {   
12  
13window.location = ieurl;   
14  
15} else {   
16  
17// it's not IE so assume it's Netscape   
18  
19window.location = nsurl;   
20  
21}   
22  
23}   
24  
25//-->   
26  
27</script>
28</head>
29<body>   
30  
31Click <a href="javascript:redirectClient('explorer.html',   
32  
33'netscape.html')">here</a>   
34  
35to redirect based on the user's browser.   
36  
37</body>
38</html>

点击 此处 去测试效果页

Published At
Categories with 网页设计
comments powered by Disqus