Sometimes we need to check the validity of the website (URL) you enter,
The code is as follows: |
Copy code |
Function CheckUrl (str ){ Var RegUrl = new RegExp (); RegUrl. compile ("^ [A-Za-z] +: // [A-Za-z0-9-_] + \. [A-Za-z0-9-_ % &? /. =] + $ "); If (! RegUrl. test (str )){ Return false; } Return true; }
|
Check whether the URL entered by the user is valid. To check the URL, you can refer to the js regular expression to verify the URL format. There are many ways to check the URL validity. For example, you can use jQuery to check the URL,
This article provides a piece of code that uses C # to determine whether the website is valid.
The following describes how to check the URL validity:
The code is as follows: |
Copy code |
Private bool UrlCheck (string strUrl) { If (! StrUrl. Contains ("http ://")&&! StrUrl. Contains ("https ://")) { StrUrl = "http: //" + strUrl; } Try { HttpWebRequest myRequest = (HttpWebRequest) WebRequest. Create (strUrl ); MyRequest. Method = "HEAD "; MyRequest. Timeout = 10000; // Timeout value: 10 seconds HttpWebResponse res = (HttpWebResponse) myRequest. GetResponse (); Return (res. StatusCode = HttpStatusCode. OK ); } Catch { Return false; } } |
When using this method, you only need to input a Url to check the validity. Remember to add the System. Net namespace when using this method.
The code is as follows: |
Copy code |
If (! UrlCheck ("http://www.111cn.net ")) { // Delete operation } |
Yes. You can enter your website on this site, but some users do not know whether to test the website or what it is. You can enter a link that cannot be accessed at a glance. Therefore, it is necessary to check the URL entered by the user. Otherwise, too many invalid links will affect the weight of the website. More importantly, too many invalid links will affect the experience of other users, because no one wants to click an invalid link.
For more information, see jQuery Ajax,
The following five methods are used to execute the short form of a general Ajax request. jQuery. Ajax () should be used to process complicated ajax requests ().
1. load (url, [data], [callback])
Load the remote HTML file code and insert it into the DOM. The GET method is used by default, and the parameter is automatically converted to the POST method when passing parameters.
Upload url: The remote url to be loaded
Transmit data: key/value data sent to the server
Callback: the callback function when the load is successful.
The sample code is as follows:
The code is as follows: |
Copy code |
// No parameter or callback function $ ("# Showload"). load ("load.htm "); // No Callback function $ ("# Showload"). load ("load.htm", {"para": "para-value "}); $ ("# Showload"). load ("load.htm", {"para": "para-value "}, Function (){ // Process }) |