我们常用的一些函数都是全局对象window下面的。这里将其梳理一下:
Timers
计时器
引用
global functions setTimeout( ), clearTimeout( ), setInterval( ), and clearInterval( ).
引用
Although the preferred way to invoke setTimeout( ) and setInterval( ) is to pass a function as the first argument, it is also legal to pass a string of JavaScript code instead.
setTimeout函数可以把函数作为参数,也可以传递JS代码的字符串
引用
One useful trick with setTimeout( ) is to register a function to be invoked after a delay of 0 milliseconds.
In practice, setTimeout( ) tells the browser to invoke the function when it has finished running the event handlers for any currently pending events and has finished updating the current state of the document.
如果将延迟时间设为0,那么绑定的函数将在文档的其他事件完成并完成更新DOM内容之后执行该函数。
详细内容,建议大家阅读以下这篇文章:
http://www.blueidea.com/tech/web/2009/6951.asp
文中得出的结论如下:
1. Javascript只有单线程,异步事件被迫排队等候执行;
2. setTimeout和setInterval在如何执行异步代码方面有根本的区别;
3. 如果计时器无法立即执行,它将延时到下一个可能的时间执行(这比预想的延迟时间要长一些);
4. 如果有充分的执行时间,Interval可能会毫无延迟的来回执行。
5. 即使将延迟设置为0,浏览器也有10ms-15ms的延迟。
Browser Location and History
浏览器地址和历史
引用
The href property of the Location object is a string that contains the complete text of the URL. The toString( ) method of the Location object returns the value of the href property, so you can use location in place of location.href.
Location对象的href属性记录了浏览器对当前页面的地址。Location对象的toString方法返回的是href属性,所以通常我们直接用Location。
引用
Other properties of this object such as protocol, host, pathname, and search specify the various individual parts of the URL
Location对象还包括定义协议、主机名、路径名等等URL不同部分的对应属性
引用
The search property contains the portion, if any, of a URL following (and including) a question mark
一段解析查询参数的函数:
/*
* This function parses ampersand-separated name=value argument pairs from
* the query string of the URL. It stores the name=value pairs in
* properties of an object and returns that object. Use it like this:
*
* var args = getArgs( ); // Parse args from URL
* var q = args.q || ""; // Use argument, if defined, or a default value
* var n = args.n ? parseInt(args.n) : 10;
*/
function getArgs( ) {
var args = new Object( );
var query = location.search.substring(1); // Get query string
var pairs = query.split("&"); // Break at ampersand
for(var i = 0; i < pairs.length; i++) {
var pos = pairs[i].indexOf('='); // Look for "name=value"
if (pos == -1) continue; // If not found, skip
var argname = pairs[i].substring(0,pos); // Extract the name
var value = pairs[i].substring(pos+1); // Extract the value
value = decodeURIComponent(value); // Decode it, if needed
args[argname] = value; // Store as a property
}
return args; // Return the object
}
引用
Although the location property of a window refers to a Location object, you can assign a string value to the property. When you do this, the browser interprets the string as a URL and attempts to load and display the document at that URL.
尽管Location是一个对象,但是你可以直接将一个URL地址的字符串传递给它,浏览器会载入该URL地址。
引用
When you call replace( ), the specified URL replaces the current one in the browser's history list rather than creating a new entry in that history list.
Location.replace()会在历史记录中替换当前条目的URL,而不是新建一个条目。
引用
document.location is a synonym for document.URL
In most cases, document.location is the same as location.href.
When there is a server redirect, however, document.location contains the URL as loaded, and location.href contains the URL as originally requested.
document.location和document.URL是一样的。
document.location在打赌属性框和location.href也是一样的。但当经过服务器页面重定向,document.location包含的是重载后的地址,但location.href包含了原来的地址
Window的常用方法
open()
close()
moveTo()
resizeBy()
resizeTo()
focus()
blur()
scrollBy( ) scrolls the document displayed in the window by a specified number of pixels left or right and up or down
scrollTo( ) scrolls the document to an absolute position
offsetLeft and offsetTop properties that specify the X and Y coordinates of the element
引用
Another approach to scrolling is to call the focus( ) method of document elements (such as form fields and buttons) that can accept keyboard focus.
scrollIntoView( ) on any HTML element to make that element visible.
focus()和scrollIntoView()方法都可以让浏览器执行滚动。
引用
cluttering up the user's browsing history with script-generated named anchors could be considered a nuisance in some situations.
window.location.replace("#top");
历史对象中过多的锚节点是让人心烦的,要注意脚本的行为。
Error Handling
错误处理
引用
The onerror property of a Window object is special. If you assign a function to this property, the function is invoked whenever a JavaScript error occurs in that window: the function you assign becomes an error handler for the window
Note that you ought to be able to simply define a global function named onerror( )
Three arguments are passed to an error handler when a JavaScript error occurs.
The first is a message describing the error.
The second argument is a string that contains the URL of the document containing the JavaScript code that caused the error.
The third argument is the line number within the document where the error occurred.
If the onerror handler returns TRue, it tells the browser that the handler has handled the error and that no further action is necessary
window.onerror属性可以指定一个函数,那么在JS发生错误时,该函数就会执行。
你也可以直接定义一个onerror()的全局函数。
有三个参数传递给了该函数:
1、错误信息
2、发生错误的文档地址
3、发生错误的代码在文档中的行数
如果该函数返回true,那么就是告诉浏览器去忽略该错误。
Relationships Between Frames
框架之间的关系
引用
Every window has a frames property. This property refers to an array of Window objects, each of which represents a frame contained within the window.
Every window also has a parent property, which refers to the Window object in which it is contained. Thus, the first frame within a window might refer to its sibling frame (the second frame within the window) like this:
parent.frames[1]
If a window is a top-level window and not a frame, parent simply refers to the window itself:
parent == self; // For any top-level window
window的frames属性包含了所有框架的window对象,每个window对象都有parent属性。parent属性指向其父窗口的window对象。那么第一个框架的窗口如果要指向第二个框架的窗口,就是
parent.frames[1]
检查是否是顶级窗口:
parent == self
Window and Frame Names
窗口和框架名
引用
When you create a frame with the HTML <frame> tag, you can specify a name with the name attribute. Those names can be used as the value of the target attribute of the <a> and <form> tags, telling the browser where you want to display the results of activating a link or submitting a form.
frame标签的name属性可以当作a和form的target属性,告诉浏览器表格和链接的目标窗口。
Determining browser vendor and version
最后一点是通过window的navigator属性判断浏览器的,这点虽然不被推荐,但是还是有参考价值的:
/**
* browser.js: a simple client sniffer
*
* This module defines an object named "browser" that is easier to use than
* the "navigator" object.
*/
var browser = {
version: parseInt(navigator. appVersion),
isNetscape: navigator.appName.indexOf("Netscape") != -1,
isMicrosoft: navigator.appName.indexOf("Microsoft") != -1
};