js 浏览器 BOM

js 浏览器 window

浏览器对象模型BOM使js有能力与浏览器对话。所有浏览器都支持window对象,它表示浏览器窗口。所有js全局变量,函数以及变量均自动成为window对象的成员。

// 确定浏览器窗口的尺寸
<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var w=window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;

var h=window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;

x=document.getElementById("demo");
x.innerHTML="Browser inner window width: " + w + ", height: " + h + "."
</script>

</body>
</html>    

js window screen

window.screen对象包含有关用户屏幕的信息。在编写时可以不使用window这个前缀。

  • screen.availWidth - 可用的屏幕宽度
  • screen.availHeight - 可用的屏幕高度
<!DOCTYPE html>
<html>
<body>

<script>

document.write("Available Width: " + screen.availWidth);
document.write(", Available Height: " + screen.availHeight);

</script>

</body>
</html>    

js window location

window.location对象用于获得当前页面的地址URL,并把浏览器重定向到新的页面。在编写时也可以不用window前缀。

  • location.hostname 返回 web 主机的域名
  • location.pathname 返回当前页面的路径和文件名
  • location.port 返回 web 主机的端口 (80 或 443)
  • location.protocol 返回所使用的 web 协议(http:// 或 https://)
  • location.href 返回当前页面的URL
  • location.assign 加载新的文档
<!DOCTYPE html>
<html>
<head>
<script>
function newDoc()
{
         window.location.assign("http://news.163.com")
}
</script>
</head>
<body>

<input type="button" value="加载新文档" onclick="newDoc()">

</body>
</html>

js window history

window.history对象包含浏览器的历史。在编写时可不使用window这个前缀。

  • history.back() - 返回加载历史列表中前一个url,与在浏览器点击后退按钮相同
  • history.forward() - 返回加载历史列表中后一个url,与在浏览器中点击按钮向前相同

js window navigator

window.navigator对象包含有关访问者浏览器的信息。在编写时可不使用window前缀。

<!DOCTYPE html>
<html>
<body>
<div id="example"></div>

<script>

txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt+= "<p>Browser Name: " + navigator.appName + "</p>";
txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt+= "<p>Platform: " + navigator.platform + "</p>";
txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";
txt+= "<p>User-agent language: " + navigator.systemLanguage + "</p>";

document.getElementById("example").innerHTML=txt;

</script>

</body>
</html>    

js 弹窗

js中可创建三种消息框:警告框,确认框,提示框。

弹窗使用\n来设置换行。例如:alert("Hellon\nHow are you?");

1)警告框用于确保用户得到某些信息。当他出现后,用户需点击确认按钮才能继续操作。

语法:window.alert("sometext");

上述方法前缀window可以不带上。

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
alert("I am an alert box!");
}
</script>
</head>
<body>

<input type="button" onclick="myFunction()" value="Show alert box">

</body>
</html>

2)确认框通常用于验证是否接受用户操作。当你点击 "确认", 确认框返回 true, 如果点击 "取消", 确认框返回 false。

语法:window.confirm("sometext");

上述方法前缀window可以不带上。

<!DOCTYPE html>
<html>
<body>

<p>Click the button to display a confirm box.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction()
{
var x;
var r=confirm("Press a button!");
if (r==true)
  {
  x="You pressed OK!";
  }
else
  {
  x="You pressed Cancel!";
  }
document.getElementById("demo").innerHTML=x;
}
</script>

</body>
</html>    

3)提示框经常用于提示用户在进入页面前输入某个值。如果用户点击确认,那么返回值为输入的值。如果用户点击取消,那么返回值为 null。

语法:window.prompt("sometext","defaultvalue");

上述方法中前缀window可以不带上。

<!DOCTYPE html>
<html>
<body>

<p>点击按钮查看输入的对话框。</p>

<button onclick="myFunction()">点我</button>

<p id="demo"></p>

<script>
function myFunction()
{
var x;

var person=prompt("请输入你的名字","Harry Potter");

if (person!=null)
  {
  x="你好 " + person + "! 今天感觉如何?";
  document.getElementById("demo").innerHTML=x;
  }
}
</script>

</body>
</html>

js 计时事件

通过使用 JavaScript,我们有能力作到在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行。我们称之为计时事件。

  • setInterval() - 间隔指定的毫秒数不停地执行指定的代码。
  • setTimeout() - 暂停指定的毫秒数后执行指定的代码

setInteval语法:window.setInterval("javascript function",milliseconds);

前缀window可省略。

<!DOCTYPE html>
<html>
<body>

<p>A script on this page starts this clock:</p>
<p id="demo"></p>

<script>
var myVar=setInterval(function(){myTimer()},1000);

function myTimer()
{
var d=new Date();
var t=d.toLocaleTimeString();
document.getElementById("demo").innerHTML=t;
}
</script>

</body>
</html>    

clearInteval()方法用于停止setInterval()方法执行的函数代码。

语法:window.clearInterval(intervalVariable)

前缀window可以省略。要使用此方法,在创建setInterval()时必须使用全局变量。

myVar=setInterval("javascript function",milliseconds);

<!DOCTYPE html>
<html>
<body>

<p>A script on this page starts this clock:</p>
<p id="demo"></p>
<button onclick="myStopFunction()">Stop time</button>

<script>
var myVar=setInterval(function(){myTimer()},1000);
function myTimer()
{
var d=new Date();
var t=d.toLocaleTimeString();
document.getElementById("demo").innerHTML=t;
}
function myStopFunction()
{
clearInterval(myVar);
}
</script>

</body>
</html>

setTimeout语法:window.setTimeout("javascript 函数",毫秒数);

<!DOCTYPE html>
<html>
<body>

<p>Click the button to wait 3 seconds, then alert "Hello".</p>
<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
setTimeout(function(){alert("Hello")},3000);
}
</script>

</body>
</html>    

clearTimeout() 方法用于停止执行setTimeout()方法的函数代码。

语法:window.clearTimeout(timeoutVariable)

前缀window可以省略。要使用clearTimeout() 方法, 你必须在创建超时方法中(setTimeout)使用全局变量。

myVar=setTimeout("javascript function",milliseconds);

<!DOCTYPE html>
<html>
<body>

<p>Click the first button alert "Hello" after waiting 3 seconds.</p>
<p>Click the second button to prevent the first function to execute. (You must click it before the 3 seconds are up.)</p>
<button onclick="myFunction()">Try it</button>
<button onclick="myStopFunction()">Stop the alert</button>

<script>
var myVar;

function myFunction()
{
myVar=setTimeout(function(){alert("Hello")},3000);
}

function myStopFunction()
{
clearTimeout(myVar);
}
</script>

</body>
</html>

js cookies

cookies用于存储web页面的用户信息。当 web 服务器向浏览器发送 web 页面时,在连接关闭后,服务端不会记录用户的信息。cookies可以记录用户名字,访问记录。

cookies以名/值对的形式存储。

当浏览器从服务器上请求 web 页面时, 属于该页面的 cookies 会被添加到该请求中。服务端通过这种方式来获取用户的信息。

JavaScript 可以使用document.cookie属性来创建 、读取、及删除 cookies。

创建:document.cookie="username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 GMT; path=/";

读取:var x = document.cookie;

修改:document.cookie="username=John Smith; expires=Thu, 18 Dec 2013 12:00:00 GMT; path=/";

删除:document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 GMT";

<!DOCTYPE html>
<html>
<head>
<script>

function setCookie(cname,cvalue,exdays)
{
var d = new Date();
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname+"="+cvalue+"; "+expires;
}

function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(‘;‘);
for(var i=0; i<ca.length; i++)
  {
  var c = ca[i].trim();
  if (c.indexOf(name)==0) return c.substring(name.length,c.length);
  }
return "";
}

function checkCookie()
{
var user=getCookie("username");
if (user!="")
  {
  alert("Welcome again " + user);
  }
else
  {
  user = prompt("Please enter your name:","");
  if (user!="" && user!=null)
    {
    setCookie("username",user,30);
    }
  }
}

</script>
</head>
<body onload="checkCookie()">
</body>
</html>
时间: 2024-08-02 17:41:46

js 浏览器 BOM的相关文章

JavaScript基础16——js的BOM对象

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>js的BOM对象</title> 6 <script type="text/javascript"> 7 // BOM:Broswer Object Model 浏览器对象模型 8 /* 9 navifator 获取客户端(浏览器)的信息 10

js浏览器各种位置检测

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>属性检测</title> <style> *{margin: 0;padding:0;border:none;} #tip{ width:400px; height:300px; } #T{ width:400px; height:200px;

分享一个控制JS 浏览器缓存的解决办法。

 JS 缓存的问题一直都是我们又爱又恨的东西.也是我们比较头痛的问题, 一方面为了提高网站响应速度,减少服务器的负担,和节省带宽,将需要将静态资源缓存在客户端, 但是另一方面,当js 文件有改动的时候,如何快速的将客户端缓存的js文件都失效,这是非常头痛的问题. 以至于每次客户反馈问题的时候,我们第一个解决办法都是清理浏览器缓存. 那么如何解决呢. 1. 直接禁止全部的静态文件缓存 在html 头部加上如下代码: <META HTTP-EQUIV="pragma" CONTENT

JS之BOM

JS语言是BS开发中负责页面动态效果的一种脚本语言,既然如此,自然少不了有关浏览器页面的知识,BOM:浏览器对象模型,涉及到有关JS中操作浏览器页面的一些基本知识.在这里最核心的对象就是window,它包含六大属性,其中这些属性本身也是对象. 通过图中,可以看出来,在window中我们最常用的就是window中的document对象即DOM(后面单独总结).这里仅简单的总结一下有关window对象中的相关方法和使用. 一.系统对话框 此方法主要用于与用户交互,起到一个提示用户的作用.在BOM弹出

实用JS系列——BOM常用对象

背景:  最近在着手项目的时候,意识到自己JS的欠缺.虽然看了不少JavaScript的视频,但真正项目中并不是经常遇到大且难的例子.所以JavaScript的基础还需要再打扎实,也就有了这一系列博客.这一系列更重视实用,理论部分可参看之前博客. BOM(Browser Object Mode)浏览器对象模型,是Javascript的重要组成部分.它提供了一系列对象用于与浏览器窗口进行交互,这些对象通常统称为BOM. 由图中可看出,window对象是BOM中所有对象的父对象. 1.window对

FileSaver.js 浏览器导出Excel文件

限制一:不同浏览器对 blob 对象有不同的限制 具体看看下面这个表格(出自 FileSaver.js): Browser Constructs as Filenames Max Blob Size Dependencies Firefox 20+ Blob Yes 800 MiB None Firefox < 20 data: URI No n/a Blob.js Chrome Blob Yes 500 MiB None Chrome for Android Blob Yes 500 MiB

js笔记--BOM编程

1.window对象 BOM的核心对象是window,它表示浏览器的一个实例.在浏览器中,window对象具有双重角色.它既是通过JS访问浏览器窗口的一个接口,又是ECMAScript规定的Global对象.所以网页中定义的任何一个对象,变量和函数,都以window作为其Global对象. 1.全局作用域 所有在全局作用域中声明的变量,函数都会编程window对象的属性和方法.但是定义全局变量和直接在window对象上定义属性还是有区别的:全局变量不能通过delete操作符删除,但是直接在win

JS之BOM和DOM(来源、方法、内容、应用)

1.Javascript组成 JavaScript的实现包括以下3个部分: 1)核心(ECMAScript):描述了JS的语法和基本对象. 2)文档对象模型 (DOM):处理网页内容的方法和接口 3)浏览器对象模型(BOM):与浏览器交互的方法和接口 ECMAScript扩展知识: ① ECMAScript是一个标准,JS只是它的一个实现,其他实现包括ActionScript. ② "ECMAScript可以为不同种类的宿主环境提供核心的脚本编程能力--",即ECMAScript不与具

js操作bom和dom

Bom 概念 BOM : Browser Object Model 浏览器对象模型,描述与浏览器进行交互的方法和接 口, ECMAscript是javascript的核心,但如果要在web中使用javascript,那么 BOM则无疑才是真正的核心. BOM提供了很多对象,用于访问浏览器的功能,这些功能与任何网页内容无关. window对象 窗口高度 var  a  = window.innerheight 窗口宽度 var a = window.innerwidth 打开一个新的页面 open