1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
---
title: Window.navigator
slug: Web/API/Window/navigator
translation_of: Web/API/Window/navigator
---
<div>{{APIRef}}</div>
<p>The <code>Window.navigator</code> read-only property returns a reference to the {{domxref("Navigator")}} object, which can be queried for information about the application running the script.</p>
<h2 id="Example" name="Example">語法</h2>
<pre class="syntaxbox"><em>navigatorObject<code> = window.navigator</code></em></pre>
<h2 id="Specification" name="Specification">範例</h2>
<h3 id="Example_1_Browser_detect_and_return_a_string">Example #1: Browser detect and return a string</h3>
<pre class="brush: js">var sBrowser, sUsrAg = navigator.userAgent;
if(sUsrAg.indexOf("Chrome") > -1) {
sBrowser = "Google Chrome";
} else if (sUsrAg.indexOf("Safari") > -1) {
sBrowser = "Apple Safari";
} else if (sUsrAg.indexOf("Opera") > -1) {
sBrowser = "Opera";
} else if (sUsrAg.indexOf("Firefox") > -1) {
sBrowser = "Mozilla Firefox";
} else if (sUsrAg.indexOf("MSIE") > -1) {
sBrowser = "Microsoft Internet Explorer";
}
alert("You are using: " + sBrowser);</pre>
<h3 id="Example_2_Browser_detect_and_return_an_index">Example #2: Browser detect and return an index</h3>
<pre class="brush: js">function getBrowserId () {
var
aKeys = ["MSIE", "Firefox", "Safari", "Chrome", "Opera"],
sUsrAg = navigator.userAgent, nIdx = aKeys.length - 1;
for (nIdx; nIdx > -1 && sUsrAg.indexOf(aKeys[nIdx]) === -1; nIdx--);
return nIdx
}
console.log(getBrowserId());</pre>
<h2 id="Specification" name="Specification">規範</h2>
<ul>
<li>{{SpecName("HTML5 W3C", "webappapis.html#the-navigator-object","window.navigator")}}</li>
<li>{{SpecName("HTML5.1", "webappapis.html#the-navigator-object", "window.navigator")}}</li>
<li>{{SpecName("HTML WHATWG", "timers.html#the-navigator-object", "window.navigator")}}</li>
</ul>
<h2 id="See_also" name="See_also">參見</h2>
|