Javascript checks whether the implementation code is connected to the Internet, and javascript is connected to the Internet
The simplest and most crude method is to load network resources, JS files, or image files.
Copy codeThe Code is as follows:
<Script src = "// ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script>
Typeof window. jQuery = "undefined" // return false or ture
Use jQuery variables to detect networking
function doConnectFunction() { return true;}function doNotConnectFunction() { return false;} var i = new Image();i.onload = doConnectFunction;i.onerror = doNotConnectFunction;i.src = 'http://su.bdimg.com/static/superplus/img/logo_white.png?d=' + escape(Date());
The problem of loading network resources is the detection of the internet, and local area network detection is powerless.
In this case, a better solution is required, and navigator. onLine is required. This attribute is relatively poor in browser compatibility, and chrome and Safari are fully supported. IE7 and above are supported. Firefox and IE6 are relatively difficult. false is returned only when the browser is "offline", and true is returned for others. If the network cable is disconnected, it is true, and Opera does not support it.
Therefore, you must add a compatibility method: Send an http header request to the location. hostname address. The Code is as follows:
var xhr = new ( window.ActiveXObject || XMLHttpRequest )( "Microsoft.XMLHTTP" );var status;xhr.open( "HEAD", "//" + window.location.hostname + "/?rand=" + Math.floor((1 + Math.random()) * 0x10000), false ); try { xhr.send(); return ( xhr.status >= 200 && xhr.status < 300 || xhr.status === 304 ); } catch (error) { return false; }
Note that the third parameter of the open method must be set to false and must be a synchronous request.
Conclusion: browsers that support navigator. onLine use navigator. onLine. If not, an http header request is sent.
Original article, reprinted Please note: Reprinted from front-end development
How can I determine whether to use javascript to detect network connectivity?
If (window. navigator. onLine = true ){
// Online
}
Else {
// Offline
}
Javascript checks user input information
1. The client can detect user input using scripts, and use regular expressions for email and English letters.
Check function:
Function CheckForm ()
{
Return email. value! = "" & Name. value! = "" & (/^ ([A-zA-Z0-9] + [_ | \-| \.]?) * [A-zA-Z0-9] + @ ([a-zA-Z0-9] + [_ | \-| \.]?) * [A-zA-Z0-9] + \. [a-zA-Z] {2, 3} $/gi ). test (email. value) & (/[a-zA-Z]/). test (name. value );
}
Write onsubmit = "return CheckForm ()" in the onsubmit event of form ()"
2. To ensure security, if all the code entered by the client is incorrect, server verification is required. verify the correctness of the Email and name after the servlet accepts the submitted data. If not, a prompt is returned.
3. Only inputs verified by the server can be saved to the database.