`
robinqu
  • 浏览: 88644 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

JavaScript window下面的常用函数、属性

阅读更多
我们常用的一些函数都是全局对象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
};

分享到:
评论

相关推荐

    JavaScript调用window函数.docx

    JavaScript调用window函数.docx

    Javascript调用C#函数示例程序

    一个在HTML中使用Javascript的window.external调用C#内函数的示例程序。该方法可应用于网页程序开发中的网页-程序沟通,例如用C#的IHTMLWindow2.execScript替换HTML默认Javascript方法使其直接与C#传参、通讯。

    javascript函数的解释

    javascript函数的解释,解释了具体函数的功能,一、函数JavaScript函数集合 1.document.write(""); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document-&gt;html-&gt;(head,body) 4.一个浏览器窗口中的DOM顺序是:...

    重写javascript中window.confirm的行为

    javascript中window.confirm这个方法很好用,可以弹出一个确认对话框我们之所以弹出这个对话框,可能就是因为该操作很危险,所以要用户确认。但如果默认选择”确定”,则可能违背了这个原则。 另外,confirm对话框的...

    JavaScript Window浏览器对象模型方法与属性汇总

    所有 JavaScript 全局对象、函数以及变量均自动成为 window 对象的成员。 全局变量是 window 对象的属性。 全局函数是 window 对象的方法。 1. open方法 语法格式: window.open(URL,窗口名称,窗口风格) 功能:打开...

    javascript常用对象梳理

    熟练掌握window对象的status、location、name、self、opener属性的使用 Window对象是客户端javascript最高层对象之一,只要打开浏览器窗口,不管该窗口中是否有打开的网页,当遇到BODY、FRAMESET或FRAME元素时,...

    javascript强制函数自动执行.pdf

    javascript强制函数自动执行 function myTest(){ window.alert("hello"); } var myTest = function(){ window.alert("hello"); }

    常用javascript整理

    1.JavaScript的数值处理对象学习 .txt 2.JavaScript的系统函数学习 .txt 3.js中用于对象的语句——with和for...in语句学习专题.txt 4.smallSoftkey小软键盘,大键盘 5.window.open参数详解 .txt 6....

    JavaScript Window – 浏览器对象模型

    JavaScript Window – 浏览器对象模型 浏览器对象模型 (BOM) 使 JavaScript 有能力与浏览器”...所有 JavaScript 全局对象、函数以及变量均自动成为 window 对象的成员。 全局变量是 window 对象的属性。 全局函数是

    javascript针对不确定函数的执行方法

    另一个方法是使用符号属性的方式来访问函数,因为函数都是window对象的属性。 利用window[函数名] 来代表该function对象,用window[函数名]()来执行或调用该函数。 例子: &lt;html&gt; &lt;head&gt; &lt;meta ...

    JavaScript中Window.open参数

    JavaScript中Window.open参数详解 1、最基本的弹出窗口代码 2、经过设置后的弹出窗口 3、用函数控制弹出窗口 4、同时弹出2个窗口 5、主窗口打开文件1.htm,同时弹出小窗口page.html ...

    JavaScript语言参考手册

    本章包含了所有 JavaScript 中未与任何对象关联的函数。 addClient addResponseHeader blob callC debug deleteResponseHeader escape eval flush getOptionValue getOptionValueCount isNaN Number parseFloat ...

    javaScript常用事件

    常用javaScript事件归纳。 常用事件: 1.onclick 鼠标单击事件 通常在下列基本对象中产生: button(按钮对象) checkbox(复选框)或(检查列表框) radio (单选钮) reset buttons(重要按钮) submit ...

    qt 调用javascript函数 带参数

    在qt中调用javascript 已在linux中编译好 qt4.5

    javascript自执行函数

    function (window, $, undefined) { play=function(){ $("#demo").val("This is a demo.");...这点非常有用也是一个 JS 框架必须支持的功能,jQuery 被应用在成千上万的 JavaScript 程序中,必须确保 jQu

    js函数大全 javascript

    javascript函数大全 1.document.write(""); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document-&gt;html-&gt;(head,body) 4.一个浏览器窗口中的DOM顺序是:window-&gt;(navigator,screen,history,location,document)...

    javascript知识点图解

    javascript 知识点图解 JavaScript 数据类型 、JavaScript 变量、Javascript 运算符、JavaScript 数组、JavaScript 函数基础、DOM 基本操作、Window 对象、JavaScript 字符串函数、正则表达式

    javascript学习笔记.docx

    f) 继承:只是在查询一个属性时自动发生,而不会在写属性时发生,就是说单写一个父类的属性时,JavaScript环境会为对象本身创建一个同名的属性,从此该属性就覆盖了父类中的属性。 12) 创建一个数组可用 new Array()...

    JAVASCRIPT闭包详解

    闭包是 ECMAScript (JavaScript)最强大的特性之一,但用好闭包的前提是必须 理解闭包。闭包的创建相对容易,人们甚至会在不经意间创建闭包,但这些无意创建的闭包却存在潜在的危害 ,尤其是在比较常见的浏览器环境...

    [hook.js]通用Javascript函数钩子

    context[Object|opt]:目标函数所在对象,用于hook非window对象下的函数,如String.protype.slice,carInstance1 methodName[String|opt]:匿名函数需显式传入目标函数名eg:this.Begin = function(){....}; } [bool]...

Global site tag (gtag.js) - Google Analytics