JavaScript Advanced--browser objects-location, Navigator, useragent, screen objects

Source: Internet
Author: User

Location Object
Location is used to get or set the URL of the form and can be used to resolve URLs.
Grammar:

Location. [Properties | method]

Location Object Properties Diagram:

Location Object properties:

Location Object method:

Gets the URL of the currently displayed document and outputs:

1 <script type= "Text/javascript" >2     var a = location.href; 3     document.write (a); 4 </script>

Navigator Object

Object properties:

Use the Navigator object to view browser-related information:

1 <script type= "Text/javascript" >2     var browser = navigator.appcodename; 3     var b_browser = navigator.appname; 4     var c_browser = navigator.platform; 5     var d_browser = navigator.useragent; 6     document.write (browser + "</br>" + B_browser + "<br/>" + C_browser + "<br/>" + d_browser)
   
    7 </script>
   

Operation Result:

Mozillanetscapewin32mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/41.0.2272.118 safari/53

UserAgent

Returns a string representation of the user-agent header (that is, a string that includes browser version information, etc.)

Grammar

Navigator.useragent

Several browsing user_agent., like 360 compatibility mode with IE, speed mode with the Chrom kernel.

Use useragent to determine what browser is used

1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">5 <title>Navigator</title>6 <Scripttype= "Text/javascript">7     functionValidb () {8         varu_agent=navigator.useragent;9         varB_name="not want to use the mainstream browser!"; Ten         if(U_agent.indexof ("Firefox")>-1){  One B_name="Firefox";  A }Else if(U_agent.indexof ("Chrome")>-1){  - B_name="Chrome";  - }Else if(U_agent.indexof ("MSIE")>-1&&U_agent.indexof ("Trident")>-1){  the B_name="IE (8-10)";  - } - document.write ("Browser:"+B_name+"<br>"); - document.write ("u_agent:"+u_agent+"<br>");  +     }  - </Script> + </Head> A <Body> at     <form> -         <inputtype= "button"value= "View Browser"onclick= "Validb ()" /> -     </form> - </Body> - </HTML>

Operation Result:
Browser: Chrome
u_agent:mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/41.0.2272.118 safari/537.36

Screen Object
The screen object is used to get the user's onscreen information.

Grammar:

Window.screen. Properties

Object properties:

Height and width of screen resolution
The Window.screen object contains information about the user's screen.
1. Screen.height returns the high screen resolution
2. Screen.width returns the width of the screen resolution
Attention:
1. Units are measured in pixels.
2. The Window.screen object can be written without using the window prefix.

To get the height and width of the screen, the code is as follows:

1 <script type= "Text/javascript" >2     document.write ("screen width:" +screen.width+ "PX<BR/ > " ); 3     document.write ("screen height:" +screen.height+ "px<br/>" ); 4 </script>

Operation Result:
Screen width: 1366px
Screen Height: 768px

Screen available high and wide
1. The Screen.availwidth property returns the width of the visitor's screen, in pixels, minus interface features such as the taskbar.
2. The Screen.availheight property returns the height of the visitor's screen, in pixels, minus interface features such as the taskbar.

Attention:
The taskbar's default height is not the same on different systems, and the taskbar position can be anywhere on the screen, so it's possible that the width and height are not the same.

To get the available height and width of the screen, the code is as follows:

1 <script type= "Text/javascript" >2     document.write ("Usable width:" + screen.availwidth); 3     document.write ("Available height:" + screen.availheight); 4 </script>

Different display values depending on the screen.
Operation Result:
Available width: 1366px
Available Height: 728px

JavaScript Advanced--browser objects-location, Navigator, useragent, screen objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.