Javascript checks whether the implementation code is connected to the Internet, and javascript is connected to the Internet

Source: Internet
Author: User

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.

Related Article

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.