js学习之Location 对象以及 Navigator对象

这两天一直看online小说,感觉太颓废了!作为一个毕业才一年的小伙子,我应该要有干劲,不然一直是个菜鸟怎么行呢!好吧,接着学习前端的js,学完了再去好好学习后台的SSH和mybatis之类的,现在只是项目中使用到,自己从没去深究也没系统的学习下,还是感觉啥都不会,哎!!!!:shock: 

Location 对象


把用户带到新的地址 代码来自w3school


<html>
<head>
<script type="text/javascript">
function currLocation()
{
alert(window.location)
}
function newLocation()
{
window.location="http://www.baidu.com"
}
</script>
</head>

<body>
<input type="button" onclick="currLocation()" value="显示当前的 URL">
<input type="button" onclick="newLocation()" value="改变 URL">
</body>

</html>


从新加在文档 代码来自w3school


<html>
<head>
<script type="text/javascript">
function reloadPage()
{
window.location.reload();
}
</script>
</head>
<body>

<input type="button" value="重新加载页面" onclick="reloadPage()" />

</body>
</html>


跳出框架 代码来自w3school


<html>
<head>
<script type="text/javascript">
function breakout()
  {
  if (window.top!=window.self) 
    {
    window.top.location="http://www.baidu.com"
    }
  }
</script>
</head>
<body>

<input type="button" onclick="breakout()" value="跳出框架!">

</body>
</html>


打开新的窗口(锚的数组) 代码来自w3school


<html>
<head>
<script type="text/javascript">
function linkTo(y)
{
var x=window.open("http://www.baidu.com","","scrollbars=yes,width=250,height=200")
x.location.hash=y
}
</script>
</head>

<body>
<h3>Links and Anchors</h3>
<p>点击一个按钮,来显示第二个窗口中的锚。</p>
<input type="button" value="0" onclick="linkTo(0)">
<input type="button" value="1" onclick="linkTo(1)">
<input type="button" value="2" onclick="linkTo(2)">
<input type="button" value="3" onclick="linkTo(3)">
</body>

</html>




Navigator对象


检测访问者的浏览器和版本号 代码来自w3school


<html>
<body>
<script type="text/javascript">
var browser=navigator.appName
var b_version=navigator.appVersion
var version=parseFloat(b_version)
document.write("浏览器名称:"+ browser)
document.write("<br />")
document.write("浏览器版本:"+ version)
</script>
</body>
</html>


有关浏览器的更多信息 代码来自w3school


<html>
<body>
<script type="text/javascript">
document.write("<p>浏览器:")
document.write(navigator.appName + "</p>")

document.write("<p>浏览器版本:")
document.write(navigator.appVersion + "</p>")

document.write("<p>代码:")
document.write(navigator.appCodeName + "</p>")

document.write("<p>平台:")
document.write(navigator.platform + "</p>")

document.write("<p>Cookies 启用:")
document.write(navigator.cookieEnabled + "</p>")

document.write("<p>浏览器的用户代理报头:")
document.write(navigator.userAgent + "</p>")
</script>
</body>
</html>




有关访问者的浏览器的全部细节 代码来自w3school


<html>
<body>

<script type="text/javascript">
var x = navigator;
document.write("CodeName=" + x.appCodeName);
document.write("<br />");
document.write("MinorVersion=" + x.appMinorVersion);
document.write("<br />");
document.write("Name=" + x.appName);
document.write("<br />");
document.write("Version=" + x.appVersion);
document.write("<br />");
document.write("CookieEnabled=" + x.cookieEnabled);
document.write("<br />");
document.write("CPUClass=" + x.cpuClass);
document.write("<br />");
document.write("OnLine=" + x.onLine);
document.write("<br />");
document.write("Platform=" + x.platform);
document.write("<br />");
document.write("UA=" + x.userAgent);
document.write("<br />");
document.write("BrowserLanguage=" + x.browserLanguage);
document.write("<br />");
document.write("SystemLanguage=" + x.systemLanguage);
document.write("<br />");
document.write("UserLanguage=" + x.userLanguage);
</script>

</body>
</html>



根据浏览器提醒用户 代码来自w3school


<html>
<head>
<script type="text/javascript">
function detectBrowser()
{
var browser=navigator.appName
var b_version=navigator.appVersion
var version=parseFloat(b_version)
if ((browser=="Netscape"||browser=="Microsoft Internet Explorer") && (version>=4))
  {alert("您的浏览器已经很棒了!")}
else
  {alert("您的浏览器需要升级了!")}
}
</script>
</head>

<body onload="detectBrowser()">
</body>

</html>

你可能感兴趣的:(JavaScript)