14.1 计时器1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24/*
* Schedule an invocation or invocations of f() in the future.
* Wait start milliseconds, then call f() every interval milliseconds,
* stopping after a total of start+end milliseconds.
* If interval is specified but end is omitted, then never stop invoking f.
* If interval and end are omitted, then just invoke f once after start ms.
* If only f is specified, behave as if start was 0.
* Note that the call to invoke() does not block: it returns right away.
*/
function invoke(f, start, interval, end) {
if (!start) start = 0; // Default to 0 ms
if (arguments.length <= 2) // Single-invocation case
setTimeout(f, start); // Single invocation after start ms.
else { // Multiple invocation case
setTimeout(repeat, start); // Repetitions begin in start ms
function repeat() { // Invoked by the timeout above
var h = setInterval(f, interval); // Invoke f every interval ms.
// And stop invoking after end ms, if end is defined
if (end) setTimeout(function() {
clearInterval(h);
}, end);
}
}
}
14.2 浏览器定位和导航
URL分解属性:protocol, host, hostname, port, pathname, search, hash
Extracting arguments from the search string of a URL1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23/*
* 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 = urlArgs(); // 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 urlArgs() {
var args = {}; // Start with an empty object
var query = location.search.substring(1); // Get query string, minus '?'
var pairs = query.split("&"); // Split at ampersands
for (var i = 0; i < pairs.length; i++) { // For each fragment
var pos = pairs[i].indexOf('='); // Look for "name=value"
if (pos == -1) continue; // If not found, skip it
var name = pairs[i].substring(0, pos); // Extract the name
var value = pairs[i].substring(pos + 1); // Extract the value
value = decodeURIComponent(value); // Decode the value
args[name] = value; // Store as a property
}
return args; // Return the parsed arguments
}
14.2.2 载入新的文档
Location对象的assign()方法可以使窗口载入并显示你指定的URL中的文档。replace()方法也类似,但它在载入新文档之前会从浏览历史中把当前文档删除。如果脚本无条件地载入一个新文档,replace()方法可能是比assgin()方法更好的选择。否则,“后退”按钮会把浏览器带回到原始文档,而相同的脚本则会再次载入新文档。如果检测到用户
的浏览器不支持某些特性来显示功能齐全的版本,可以用location.replace()来载入静态的HTML版本。1
2
3
4
5
6
7location = "http://www.oreilly.com"; // Go buy some books!
location = "page2.html"; // Load the next page
/**
* #top不存在时,浏览器跳到文档开始处
*/
location = "#top"; // Jump to the top of the document
location.search = "?page=" + (pagenum+1); // load the next page
14.3 浏览历史1
2
3
4history.back();
history.forward();
history.go(0);
history.go(-2);
14.4 浏览器和屏幕信息
14.4.1 Navigator对象
4个属性
- appName
- appVersion
- userAgent
- platform
- 一下是未标准化的属性
- onLine //是否连接到网络
- geolocation //确定用户地理位置的接口
- javaEnabled() //浏览器可以运行Java小程序是返回
true
- cookiesEnabled() //浏览器可以永久保存cookies时返回
true
14.5 对话框
alert()
confirm()
prompt()
showModalDialog()
1 | <!-- |
14.8 多窗口和窗体
14.8.1 打开和关闭窗口
window.open()
window.close()
函数在定义它的作用域中执行,而不是在调用它的作用域中执行