JavaScript in BOM operations

The method of operating a browser JavaScript, the method provided by the browser, it is necessary to consider compatibility

1, the prompt box / box warning

// output pop 
window.alert ( 'pop-up warning \ n follows \' I am warnings \ '')

2, the input box
pop up an input box for receiving input data, wherein () is prompted to accept the need to enter the contents,
windows.prompt ( 'prompt content')

// prompted to enter an account 
var the userName = window.prompt ( "Please enter the account ' );
 // prompted for a password 
var UserPwd = window.prompt (" Please enter your password' );
console.log(userName,userPwd);

Data input, storage mode, the string type. If the input value needs to be calculated, the data types must be converted into numerical
Method 1: parseInt () Gets the integer part

var = RES1 = the parseInt (window.prompt ( 'Enter password' ));
console.log(res1);

Method 2: performing non-addition does not affect the calculation of the value

// subtract zero 
var RES2 = window.prompt ( 'Enter Password') - 0 ;
 // Result 1 by 
var RES2 = window.prompt ( 'Enter Password') * 1 ;
 // results except 1 
var RES2 = window.prompt ( 'enter password') / 1;

Method 3: before the number, plus add + / - Symbol

// - Symbols: negative results obtained negative positive positive result is obtained 
var RES2 = -window.prompt ( "Please enter your password ' );
console.log(res2)
// + positive sign: positive results obtained positive negative negative result is acquired 
var RES2 = + window.prompt ( "Please enter your password ' );
console.log(res2)

3, confirmation box

window.confirm ( 'prompt content' );
Click the OK button, the result returned is true
Click the Cancel button, the result returned is false
// For the alert operation determines 
var RES = window.confirm ( 'ask whether or not to delete' );
console.log(res);

4, the escape character
similar to HTML character entities in order to solve the conflict and the contents of the input JavaScript code
, for example, playing the box to achieve wrap

\ N newline
\ '     Single quote
\" Double quotes
(The first three commonly used, the latter is not used)
\ R carriage return
\ T horizontal tab (Ctrl-I)
\\ backslash
\ F longitudinally spaced

5, get the browser scroll bar

Get a scroll bar that is get the browser scroll bar height, width.
Press There are no document type declaration: <! DOCTYPE html>

// There Declare the use 
document.documentElement.scrollTop;
 // do not use 
document.body.scrollTop;

If you get the wrong way, the program does not complain, just get the result is zero

var height1 = document.documentElement.scrollTop;
 var height2 = document.body.scrollTop;
 // wherein a result is 0 
the console.log (height1, height2);

Example:

// height of the volume on the browser 
var height = document.documentElement.scrollTop || document.body.scrollTop
 // browser of the mobile width 
var width = document.documentElement.scrollLeft || document.body.scrollLeft;
console.log(height , width);

It can also be used to set properties

// set scrollbar width and height 
// set value can be directly, do not add PX 
document.documentElement.scrollTop = 500 ;
document.documentElement.scrollLeft = 500;

Guess you like

Origin www.cnblogs.com/karl-kidd/p/12578838.html