This article will talk about the benefits of turning on JavaScript in the browser and how to set up the browser to support JavaScript. I hope it will be helpful to you. Don’t forget to bookmark this site.
(1) JavaScript about browsers
JavaScript about browsers
JavaScript runs in browsers. Different browsers have different support for JavaScript. When writing JavaScript, you must consider the differences in browsers and try your best to make it run in different browsers.
1. Objects in the browser
1.window
Window: Represents the window opened by the browser and serves as the global scope for python for statement usage. There is no need to specify it when using the properties and methods of Windows objects (generally follow the custom: for example, window.alert() is usually alert()).
Attribute name |
Function |
name |
Specify the name of the window |
parent |
The parent window of the current window (frame), use it to return the methods and properties of the object |
opener |
Returns the window object that created the current window, using its methods and properties to return the object |
top |
Represents the main window, which is the top-level window and the parent window of all other windows. The methods and properties of the current window can be accessed through this object |
self |
Returns an object of the current window through which the methods and properties of the current window can be accessed |
defaultstatus |
Returns or sets the default content that will be displayed in the browser status bar |
status |
Returns or sets the specified content that will be displayed in the browser status bar |
Method name |
Function |
alert() |
Displays an alert dialog box containing a message and an OK button |
confirm() |
Show a confirmation dialog |
prompt() |
Displays a prompt dialog box prompting the user to enter data |
open() |
Open an existing window or create a new window and load a document in it |
close() |
Close an open window |
navigate() |
Display the specified web page in the current window |
setTimeout() |
Set a timer to call a function after a specified interval |
clearTimeout() |
Reset the specified timer |
focus() |
Gives a Window object the current focus |
blur() |
Causes a Window object to lose its current focus |
Event |
Description |
onload |
Occurs when an HTML file is loaded into the browser |
onunload |
Occurs when an HTML file is deleted from the browser |
onfocus |
Occurs when a window gains focus |
onblur |
Occurs when a window loses focus |
onhelp |
Occurs when the user presses the F1 key |
onresize |
Occurs when the user resizes the window |
onscroll |
Occurs when the user scrolls the window |
onerror |
Occurs when there is an error loading the HTML file |
Some operations for Window:
alert('window inner size: ' + window.innerWidth + ' x ' + window.innerHeight);//Get the window size window.confirm('Information displayed in the dialog box!'); mu = window.open("http://www.baidu.com","","width=200,height=100");//Open a web page, you can also have only the url in it, and the other defaults mu.moveTo(1600,584);//Where to move the window mu.moveBy(-111,-111);//How much is the displacement based on xy mu.resizeTo(800,400);//Change the window size to w800, h400 var a =prompt("Please enter your name", "Contents of the text box when empty");//Input prompt box and return the input content alert(a);
Some timing operations:
There are four timing operation functions: window.setInterval, window.clearInterval, window.setTimeout, and window.clearTimeout. These four functions are all methods of the window object, which shows that timing operations in the browser are completed by the browser window. (unit ms)
① window.setInterval sets the timer and executes the specified code at regular intervals window.setInterval(code,time);
② window.clearInterval clears the timer set by the setInterval function window. clearInterval(time);
③ window.setTimeout sets the timer and executes the specified code at regular intervals. window.setTimeout(code, time); the code is only executed once.
④ window.clearTimeout clears the timer set by the setTiimeout function window.clearTimeout(time);
A piece of code for dynamically displaying time: Set a timer, the timer executes the clock() function every 1000ms, and assigns the value to the text box in the function.
<html> <language=java> var int=self.setInterval("clock()",1000); function clock(){ var t=new Date(); document.getElementById("clock").value=t; }; </> <body> <input type="text" id="clock" size="35" /> <button οnclick="int=window.clearInterval(int)">Stop timer</button> </body> </html>
A code that displays the time on the page:
<html> <language=java> var int=self.setInterval("clock()",1000); function clock(){ var t=new Date(); var hehe = document.getElementById("ck"); hehe.innerText=t; }; </> <body> <p id="ck">...</p> <button οnclick="int=window.clearInterval(int)">stop</button> </body> </html>
2.navigator
The navigation object contains information about the browser itself; this information can be modified, so use this to determine what is incorrect.
The 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 + '\ ' + 'appVersion = ' + navigator.appVersion + '\ ' + 'language = ' + navigator.language + '\ ' + 'platform = ' + navigator.platform + '\ ' + 'userAgent = ' + navigator.userAgent);
3.scree
The screen object contains information about the client screen and rendering capabilities;
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)
4.location
The location object contains the browser’s current URL information;
var b = location.href;//Get the complete URL alert(b); location.protocol; // Protocol 'http' location.host; //Address 'www.xxx.com' location.port; //Port number '8080' location.pathname; // path '/path/index.html' location.search; location.hash;
The location method reloads the page, reloads the current page and loads a new page.
if (confirm('Reload current page' + location.href + '?')) { location.reload(); } else { location.assign('https://www.baidu.com/index.php?tn=monline_3_dg'); // Set a new URL address }
5.document
The document object represents the document object of the page loaded in the browser;
document.getElementById('unid');//Get this object document.getElementsByTagName('input');
The document object also has a cookie attribute, which can obtain the cookie of the current page. (The server can distinguish users from this)
document.cookie;
JavaScript can read the cookie of the page. When setting the cookie on the server side, you should always insist on using httpOnly
; this behavior is implemented by the browser. After setting, JavaScript cannot read the cookie. All mainstream browsers support httpOnly. options.
6.history
The forward and back methods are like the browser’s forward and back functions.
history.forward();//Forward history.back();//Back