JavaScript can obtain many objects provided by the browser and perform operations. This article will introduce you to the relevant knowledge of JavaScript's browser objects. Let's take a look.
JavaScript can obtain many objects provided by the browser and operate them.
window
The window object not only serves as the global scope, but also represents the browser window.
The window object has innerWidth and innerHeight properties, which can get the internal width and height of the browser window. The internal width and height refers to the net width and height used to display the web page after removing placeholder elements such as menu bars, toolbars, and borders.
Compatibility: IE<=8 is not supported.
// 可以调整浏览器窗口大小试试: alert('window inner size: ' + window.innerWidth + ' x ' + window.innerHeight);
Correspondingly, there is also an outerWidth and outerHeight attribute, which can obtain the entire width and height of the browser window.
navigator
navigator object represents browser information. The most commonly used attributes include :
navigator.appName: Browser name;
navigator.appVersion: Browser version;
navigator.language: The language set by the browser;
navigator.platform: Operating system type;
navigator.userAgent: User-Agent string set by the browser.
alert('appName = ' + navigator.appName + '\n' + 'appVersion = ' + navigator.appVersion + '\n' + 'language = ' + navigator.language + '\n' + 'platform = ' + navigator.platform + '\n' + 'userAgent = ' + navigator.userAgent);
Please note that the navigator information can be easily modified by the user, so the value read by JavaScript is not necessarily correct. In order to write different codes for different browsers, many beginners like to use if to determine the browser version, for example:
var width; if (getIEVersion(navigator.userAgent) < 9) { width = document.body.clientWidth; } else { width = window.innerWidth; }
However, this may not only make the judgment inaccurate, but also make it difficult to maintain the code. The correct method is to make full use of JavaScript's feature of returning undefined for non-existent properties, and directly use the short-circuit operator || to calculate:
var width = window.innerWidth || document.body.clientWidth;
screen
screen object represents screen information. Commonly used attributes are:
screen.width: screen width, in pixels;
screen.height: Screen height, in pixels;
screen.colorDepth: Returns the number of color digits, such as 8, 16, 24.
alert('Screen size = ' + screen.width + ' x ' + screen.height);
location
##location object represents the URL information of the current page. For example, a complete URL:
http://www.example.com:8080/path/index.html?a=1&b=2#TOP can be obtained using location.href . To get the value of each part of the URL, you can write:location.protocol; // 'http' location.host; // 'www.example.com' location.port; // '8080' location.pathname; // '/path/index.html' location.search; // '?a=1&b=2' location.hash; // 'TOP'
if (confirm('重新加载当前页' + location.href + '?')) { location.reload(); } else { location.assign('/discuss'); // 设置一个新的URL地址 }
document
The document object represents the current page. Since HTML is represented as a tree structure in the form of DOM in the browser, the document object is the root node of the entire DOM tree. The title attribute of document is read fromdocument.title = '努力学习JavaScript!';
<dl id="drink-menu" style="border:solid 1px #ccc;padding:6px;"> <dt>摩卡</dt> <dd>热摩卡咖啡</dd> <dt>酸奶</dt> <dd>北京老酸奶</dd> <dt>果汁</dt> <dd>鲜榨苹果汁</dd> </dl>
getElementsByTagName() provided by the document object to obtain a DOM node by ID and by Tag The name gets a set of DOM nodes:
var menu = document.getElementById('drink-menu'); var drinks = document.getElementsByTagName('dt'); var i, s, menu, drinks; menu = document.getElementById('drink-menu'); menu.tagName; // 'DL' drinks = document.getElementsByTagName('dt'); s = '提供的饮料有:'; for (i=0; i<drinks.length; i++) { s = s + drinks[i].innerHTML + ','; } alert(s);
Hot Mocha Coffee
Yoghurt
Beijing Old Yoghurt
Juice
freshly squeezed apple juice
## The #document object also has a cookie attribute, which can get the cookie of the current page.
Cookie is the key-value identifier sent by the server. Because the HTTP protocol is stateless, but if the server wants to distinguish which user sent the request, it can use Cookie to distinguish. When a user successfully logs in, the server sends a cookie to the browser, such as user=ABC123XYZ (encrypted string)... After that, when the browser visits the website, it will attach this cookie to the request header, and the server will use the cookie based on the cookie. users can be distinguished.
Cookies can also store some settings of the website, such as the language displayed on the page, etc.
JavaScript can read the Cookie of the current page through document.cookie:
document.cookie; // 'v=123; remember=true; prefer=zh'
Since JavaScript can read the Cookie of the page, and the user's login information usually also exists in the Cookie, this This creates a huge security risk because it is allowed to introduce third-party JavaScript code into HTML pages:
<!-- 当前页面在wwwexample.com --> <html> <head> <script src="http://www.foo.com/jquery.js"></script> </head> ... </html>
If there is malicious code in the introduced third-party JavaScript, the www.foo.com website The
user login information of the www.example.com website will be obtained directly. In order to solve this problem, the server can use httpOnly when setting Cookies. Cookies set to httpOnly will not be read by JavaScript. This behavior is implemented by the browser. All mainstream browsers support the httpOnly option, and IE supports it starting from IE6 SP1. To ensure security, the server should always insist on using httpOnly when setting cookies. history history object saves the history of the browser, JavaScript can call back() of the history object or forward (), which is equivalent to the user clicking the browser's "back" or "forward" button. This object is a historical legacy object. For modern Web pages, due to the extensive use of AJAX and page interaction, simply and rudely calling history.back() may make users very angry. When novices start designing web pages, they like to call history.back() when the login page is successfully logged in, trying to return to the page before login. This is a wrong approach. You should not use the history object under any circumstances.
The above is the detailed content of Detailed explanation of JavaScript browser object sample code. For more information, please follow other related articles on the PHP Chinese website!