1. Trang chủ
  2. » Công Nghệ Thông Tin

JavaScript Bible, Gold Edition part 205 potx

10 40 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 85,51 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Listing 28-1: Functions to Examine Browsers UserAgent Property Library // basic brand determination function isNav { return navigator.appName == “Netscape” } function isIE { return na

Trang 1

are perfectly usable in ifconditional phrases) If this kind of browser detection occurs frequently in your pages, consider moving these functions into an external

.jssource library for inclusion in your pages (see Chapter 13).

When you load this page, it presents fields that display the results of each function depending on the type of browser and client operating system you use.

Listing 28-1: Functions to Examine Browsers

<HTML>

<HEAD>

<TITLE>UserAgent Property Library</TITLE>

<SCRIPT LANGUAGE=”JavaScript”>

// basic brand determination function isNav() {

return (navigator.appName == “Netscape”) }

function isIE() { return (navigator.appName == “Microsoft Internet Explorer”) }

// operating system platforms function isWindows() { return (navigator.appVersion.indexOf(“Win”) != -1) }

function isWin95NT() { return (isWindows() && (navigator.appVersion.indexOf(“Win16”) == -1 && navigator.appVersion.indexOf(“Windows 3.1”) == -1))

}

function isMac() { return (navigator.appVersion.indexOf(“Mac”) != -1) }

function isMacPPC() { return (isMac() && (navigator.appVersion.indexOf(“PPC”) != -1 ||

navigator.appVersion.indexOf(“PowerPC”) != -1)) }

function isUnix() { return (navigator.appVersion.indexOf(“X11”) != -1) }

// browser versions function isGeneration2() { return (parseInt(navigator.appVersion) == 2) }

navigator.appCodeName

Trang 2

function isGeneration3() {

return (parseInt(navigator.appVersion) == 3)

}

function isGeneration3Min() {

return (parseInt(navigator.appVersion.charAt(0)) >= 3)

}

function isNav4_7() {

return (isNav() && parseFloat(navigator.appVersion) == 4.7)

}

function isMSIE4Min() {

return (isIE() && navigator.appVersion.indexOf(“MSIE”) != -1)

}

function isMSIE5_5() {

return (navigator.appVersion.indexOf(“MSIE 5.5”) != -1)

}

function isNN6Min() {

return (isNav() && parseInt(navigator.appVersion) >= 5)

}

// element referencing syntax

function isDocAll() {

return (document.all) ? true : false

}

function isDocW3C() {

return (document.getElementById) ? true : false

}

// fill in the blanks

function checkBrowser() {

var form = document.forms[0]

form.brandNN.value = isNav()

form.brandIE.value = isIE()

form.win.value = isWindows()

form.win32.value = isWin95NT()

form.mac.value = isMac()

form.ppc.value = isMacPPC()

form.unix.value = isUnix()

form.ver3Only.value = isGeneration3()

form.ver3Up.value = isGeneration3Min()

form.Nav4_7.value = isNav4_7()

form.Nav6Up.value = isNN6Min()

form.MSIE4.value = isMSIE4Min()

form.MSIE5_5.value = isMSIE5_5()

form.doc_all.value = isDocAll()

Continued

navigator.appCodeName

Trang 3

Listing 28-1 (continued)

form.doc_w3c.value = isDocW3C() }

</SCRIPT>

</HEAD>

<BODY onLoad=”checkBrowser()”>

<H1>About This Browser</H1>

<FORM>

<H2>Brand</H2>

Netscape Navigator:<INPUT TYPE=”text” NAME=”brandNN” SIZE=5>

Internet Explorer:<INPUT TYPE=”text” NAME=”brandIE” SIZE=5>

<HR>

<H2>Browser Version</H2>

3.0x Only (any brand):<INPUT TYPE=”text” NAME=”ver3Only” SIZE=5><P>

3 or Later (any brand): <INPUT TYPE=”text” NAME=”ver3Up” SIZE=5><P>

Navigator 4.7: <INPUT TYPE=”text” NAME=”Nav4_7” SIZE=5><P>

Navigator 6+: <INPUT TYPE=”text” NAME=”Nav6Up” SIZE=5><P>

MSIE 4+: <INPUT TYPE=”text” NAME=”MSIE4” SIZE=5><P>

MSIE 5.5:<INPUT TYPE=”text” NAME=”MSIE5_5” SIZE=5><P>

<HR>

<H2>OS Platform</H2>

Windows: <INPUT TYPE=”text” NAME=”win” SIZE=5>

Windows 95/98/2000/NT: <INPUT TYPE=”text” NAME=”win32” SIZE=5><P>

Macintosh: <INPUT TYPE=”text” NAME=”mac” SIZE=5>

Mac PowerPC: <INPUT TYPE=”text” NAME=”ppc” SIZE=5><P>

Unix: <INPUT TYPE=”text” NAME=”unix” SIZE=5><P>

<HR>

<H2>Element Referencing Style</H2>

Use <TT>document.all</TT>: <INPUT TYPE=”text” NAME=”doc_all” SIZE=5><P> Use <TT>document.getElementById()</TT>: <INPUT TYPE=”text” NAME=”doc_w3c” SIZE=5><P>

</FORM>

</BODY>

</HTML>

Sometimes you may need to use more than one of these functions together For example, if you want to create a special situation for the window.open()bug that afflicts UNIX and Macintosh versions of Navigator 2, then you have to put your Boolean operator logic powers to work to construct a fuller examination of the browser:

function isWindowBuggy() { return (isGeneration2() && (isMac() || isUnix())) }

navigator.appCodeName

Trang 4

You can see many more examples of browser sniffing, including more details about

handling AOL browsers, in an article by Eric Krock at: http://developer

netscape.com:80/docs/examples/javascript/browser_type.html.

appMinorVersion

Example

Use The Evaluator (Chapter 13) to examine the two related version properties of

your IE browser(s) Type the following two statements into the top text box and

observe the results:

navigator.appVersion

navigator.minorAppVersion

There is a good chance that the values returned are not related to the browser

ver-sion number shown after MSIEin the appVersionvalue.

cookieEnabled

Example

Use The Evaluator to see the value of the navigator.cookieEnabledproperty on

your browsers Enter the following statement into the top text box:

navigator.cookieEnabled

Feel free to change the cookie preferences setting temporarily to see the new value

of the property You do not have to relaunch the browser for the new setting to take

effect.

navigator.cookieEnabled

Trang 5

Example

Use The Evaluator (Chapter 13) to see how IE reports the cpuClassof your PC Enter the following statement into the top text box:

navigator.cpuClass

mimeTypes

Example

For examples of the mimeTypesproperty and details about using the mimeType object, see the discussion of this object later in the chapter A number of simple examples showing how to use this property to see whether the navigatorobject has a particular MIME type do not go far enough in determining whether a plug-in is installed and enabled to play the incoming data.

onLine

Example

Use The Evaluator (Chapter 13) to see the online state of your IE browsers Enter the following statement into the top text box:

navigator.onLine

navigator.onLine

Trang 6

Verify your browsing mode by checking the Work Offline choice in the File menu If

it is checked, the onLineproperty should return false.

oscpu

Example

Use The Evaluator (Chapter 13) with NN6 to see what your client machine reports

to you by entering the following statement into the top text box:

navigator.oscpu

platform

Example

Use The Evaluator (Chapter 13) to see what your computer reports as its operating

system Enter the following statement into the top text box:

navigator.platform

product

productSub

vendor

vendorSub

navigator.product

Trang 7

Use The Evaluator (Chapter 13) on your copy of NN6 to see the values returned for these four properties Enter each of the following statements into the top text box

of the page and see the values for each in the Results box:

navigator.product navigator.productSub navigator.vendor navigator.vendorSub

Also check the value of the navigator.userAgentproperty to see how many of these four property values are revealed in the userAgentproperty.

systemLanguage userLanguage

Example

Use The Evaluator (Chapter 13) with your IE4+ browser to compare the values of the three language-related properties running on your computer Enter each of the following statements into the top text box:

navigator.browserLanguage navigator.systemLanguage navigator.userLanguage

Don’t be surprised if all three properties return the same value.

Methods

preference(name [, val])

navigator.preference()

Trang 8

The page in Listing 28-2 displays checkboxes for several preference settings plus

one text box to show a preference setting value for the size of the browser’s disk

cache To run this script without signing the scripts, turn on codebase principals as

directed in Chapter 46 (The listing file on the CD-ROM does not employ signed

scripts.)

One function reads all the preferences and sets the form control values accordingly.

Another function sets a preference when you click its checkbox Because of the

interaction among three of the cookie settings, it is easier to have the script rerun

the showPreferences()function after each setting rather than you trying to

man-ually control the properties of the three checkboxes Rerunning that function also

helps verify that you set the preference.

Listing 28-2: Reading and Writing Browser Preferences

<HTML>

<HEAD>

<TITLE>Reading/Writing Browser Preferences</TITLE>

<SCRIPT LANGUAGE=”JavaScript1.2”>

function setPreference(pref, value) {

netscape.security.PrivilegeManager.enablePrivilege(

“UniversalPreferencesWrite”)

navigator.preference(pref, value)

netscape.security.PrivilegeManager.revertPrivilege(

“UniversalPreferencesWrite”)

showPreferences()

}

function showPreferences() {

var form = document.forms[0]

netscape.security.PrivilegeManager.enablePrivilege(

“UniversalPreferencesRead”)

form.imgLoad.checked = navigator.preference(“general.always_load_images”)

form.cacheSize.value = navigator.preference(“browser.cache.disk_cache_size”)

form.ssEnable.checked = navigator.preference(“browser.enable_style_sheets”)

form.autoIEnable.checked = navigator.preference(“autoupdate.enabled”)

var cookieSetting = navigator.preference(“network.cookie.cookieBehavior”)

for (var i = 0; i < 3; i++) {

form.elements[“cookie” + i].checked = (i == cookieSetting) ? true :

false

}

Continued

navigator.preference()

Trang 9

Listing 28-2 (continued)

form.cookieWarn.checked = navigator.preference(“network.cookie.warnAboutCookies”) netscape.security.PrivilegeManager.revertPrivilege(

“UniversalPreferencesRead”) }

</SCRIPT>

</HEAD>

<BODY onLoad=”showPreferences()”>

<H1>Browser Preferences Settings Sampler</H1>

<HR>

<FORM>

<INPUT TYPE=”checkbox” NAME=”imgLoad”

onClick=”setPreference(‘general.always_load_images’,this.checked)”> Automatically Load Images<BR>

<INPUT TYPE=”checkbox” NAME=”ssEnable”

onClick=”setPreference(‘browser.enable_style_sheets’,this.checked)”> Style Sheets Enabled<BR>

<INPUT TYPE=”checkbox” NAME=”autoIEnable”

onClick=”setPreference(‘autoupdate.enabled’,this.checked)”>

AutoInstall Enabled<BR>

<INPUT TYPE=”checkbox” NAME=”cookie0”

onClick=”setPreference(‘network.cookie.cookieBehavior’,0)”>

Accept All Cookies<BR>

<INPUT TYPE=”checkbox” NAME=”cookie1”

onClick=”setPreference(‘network.cookie.cookieBehavior’,1)”>

Accept Only Cookies Sent Back to Server<BR>

<INPUT TYPE=”checkbox” NAME=”cookie2”

onClick=”setPreference(‘network.cookie.cookieBehavior’,2)”>

Disable Cookies<BR>

<INPUT TYPE=”checkbox” NAME=”cookieWarn”

onClick=”setPreference(‘network.cookie.warnAboutCookies’,this.checked)”> Warn Before Accepting Cookies<BR>

Disk cache is <INPUT TYPE=”text” NAME=”cacheSize” SIZE=10> KB <BR>

</FORM>

</BODY>

</HTML>

navigator.preference()

Trang 10

screen Object

Properties

availLeft

availTop

Example

If you are a Windows user, you can experiment with these NN4+ properties via The

Evaluator (Chapter 13) With the taskbar at the bottom of the screen, enter these

two statements into the top text box:

screen.availLeft

screen.availTop

Next, drag the taskbar to the top of the screen and try both statements again Now,

drag the taskbar to the left edge of the screen and try the statements once more.

userProfile Object

Methods

addReadRequest(“attributeName”)

userProfile.addReadRequest()

Ngày đăng: 06/07/2014, 06:20