Tips for using JavaScript (. net html

Source: Internet
Author: User
Tags number strings domain name registration to domain

(1) confirm the deletion usage:
1. BtnDel. Attributes. Add ("onclick", "return confirm ('" + "are you sure you want to delete? "+ "')");
2. linktempDelete. Attributes ["onclick"] = "javascript: return confirm ('" + "are you sure you want to delete it? "+ "');";
3. private void grdProject_ItemDataBound (object sender, DataGridItemEventArgs e)
4.
{
If (e. Item. ItemType = ListItemType. Item) | (e. Item. ItemType = ListItemType. AlternatingItem ))
{
// Except the prompt section on the "push" tab
E. Item. Cells [10]. Attributes. Add ("onclick", "return confirm ('Are you sure you want to delete it? ');");
}
}
5.
<Script language = "JavaScript" type = "text/JavaScript">
Function delete_y (e)
{
If (event. srcElement. outerText = "delete ")
Event. returnValue = confirm ("are you sure you want to delete? ");
}
Document. onclick = delete_y;
</Script>
(2) Replacement of cross-language strings
System. text. regularExpressions. regex. replace (str1, @ "<{1} [^ <>] *> {1 }",""). replace ("",""). replace ("\","\\"). replace ("-","-"). replace ("&","&"). replace ("<", "<"). replace (">", "> "). replace ("br", "\ n ");
(3) Close the form
1.
This. btnClose. Attributes. Add ("onclick", "window. close (); return false ;");
2. Close this form and jump to another page
This. HyperLink1.NavigateUrl = "javascript: onclick = window. opener. location. assign
('"+" Index. aspx "+ "? & Func = Edit & AutoID = "+ intAutoid + '); window. close ();";
3. Close the parent form:
<INPUT type = "reset" value = '<% = this. _ Cancel %> 'onclick = "window. parent. close ()" Class = "Button">
4. Close the pop-up form and refresh the parent page.
This. Response. Write ("<script language = 'javascript '> window. opener. location. reload (); window. close (); </script> ");
(4). Web MessageBox
1.
Response. Write ("<script language = javascript> window. alert ('saved successfully 1'); </script> ");
2.
Response. Write ("<script> alert ('" + "saved successfully! "+" '); </Script> ");
3.
This. Page. RegisterStartupScript ("ChiName", "<script language = 'javascript '> alert ('" + "saved successfully! "+" ') </Script> ");
(5) Add the CheckBox control template column in The DataGrid.
See: http://blog.csdn.net/chengking/archive/2005/10/08/497520.aspx
(6). window. open () method
Syntax: window. open (pageurl, name, parameters );
The open () method of the window object is used to create a new window instance. The appearance of the newly created window is specified by the parameter parameters. The document opened in the new window is specified by the parameter pageurl. The system can access the window according to the name specified by parameter: name.
The following table lists the parameters:
Description of the start and end Parameters
AlwaysLowered yes/no specifies that the window is hidden under all windows.
AlwaysRaised yes/no specifies that the window floats above all windows.
Dependent yes/no specifies that the window opened is a window of the parent window. And close with the closure of the parent window.
Directions yes/no specifies whether the directory columns of Navigator 2 and 3 are visible in the new window.
Height pixel value sets the pixel height of the new window.
Hotkeys yes/no. In the new window without the menu bar, set the Security Exit hotkey.
InnerHeight pixel value sets the pixel height of the document in the new window.
InnerWidth pixel value sets the pixel width of the document in the new window.
Location yes/no indicates whether the location bar is visible in the new window.
Menubar yes/no indicates whether the menu bar is visible in the new window.
OuterHeight pixel value sets the pixel height of the window (including the decorative border.
OuterWidth pixel value sets the pixel width of the window (including the decorative border.
Resizable yes/no indicates whether the new window can be adjusted.
ScreenX pixel value sets the pixel length of the new window from the screen boundary.
ScreenY pixel value specifies the pixel length between the new window and the upper boundary of the screen.
Scrollbars yes/no indicates whether the scroll bar is visible in the new window.
Titlebar yes/no indicates whether the menu question bar is visible in the new window.
Toolbar yes/no indicates whether the toolbar is visible in a new window.
Width pixel value specifies the pixel Width of the window.
Z-look yes/no contains an array of <pplet> labels in the document.
Whether the fullscreen yes/no opened form is displayed in full screen
Left pixel value indicates the distance between the new window and the left of the screen.
Top pixel value specifies the distance from the new window to the top of the screen
Example:
<Html>
<Head>
<Title> window. open function </title>
</Head>
<Body>
<Script language = "javascript">
<! --
Window. open ("", "name1", "width = 100, height = 200, toolbar = no, scrollbars = no, menubar = no, screenX = 100, screenY = 100 ");
// -->
</Script>
</Body>
</Html>
(7). location object
The entire URL string of href.
The protocol contains the first part of the URL, such as http:
Host contains a string containing the host name in the URL: port number, for example, // www.cenpok.net/server/
Hostname a string containing the host name in the URL. Such as a http://www.cenpok.net
Port contains the possible port number strings in the URL.
The part after "/" in the pathname URL. For example ~ List/index.htm
String after hash "#" (CGI parameter.
Search "? "# (CGI parameter.
(8). Button capture
1. Ctrl + Enter key capture method
<Body onkeydown = "doKeyDown ()">
<Script language = "JavaScript">
<! --
Function doKeyDown ()
{
If (event. ctrlKey & event. keyCode = 13)
{
Alert ("You pressed the Ctrl + Enter ")
}
}
// -->
</Script>
</Body>
2. Alt + shortcut: Alt +
<Button accessKey = A title = "Alt + A" onclick = "alert ('button clicked! ') "> Alt + A </button>
(9). Control Input. invalid characters cannot be entered into TextBox.
<Asp: textbox class = "Text"
Onkeypress = "if (event. keyCode <48 | event. keyCode> 57) event. returnValue = false ;"
Id = "txtY_Revenue" style = "TEXT-ALIGN: right" runat = "server" Width = "90%" MaxLength = "12">
</Asp: textbox>
Note: This method controls TextBox to receive only numbers: 0 ~ 9. You can also customize other input characters, such as: 65 ~ 123, only ~ Z and ~ Z and so on.
[Add to favorites as follows]
1> shielding function
1.1 shield all keyboard keys
<Script language = "javascript">
<! --
Function document. onkeydown (){
Event. keyCode = 0;
Event. returnvalue = false;
}
-->
</Script>
1.2 Right-click the screen
Add oncontextmenu = self. event. returnvalue = false to the body tag.
Or
<Script language = "javascript">
<! --
Function document. oncontextmenu ()
{
Return false;
}
-->
</Script>
Function nocontextmenu ()
{
If (document. all ){
Event. cancelBubble = true;
Event. returnvalue = false;
Return false;
}
}
Or
<Body onmousedown = "rclick ()" oncontextmenu = "nocontextmenu ()">
<Script language = "javascript">
<! --
Function rclick ()
{
If (document. all ){
If (event. button = 2 ){
Event. returnvalue = false;
}
}
}
-->
</Script>
1.3 Block Ctrl + N, Shift + F10, F5 refresh, and return keys
<Script language = "javascript">
<! --
// Shield right-click, Ctrl + N, Shift + F10, F5 refresh, and backspace key
Function window. onhelp () {return false} // block F1 help
Function KeyDown (){
If (window. event. altKey )&&
(Window. event. keyCode = 37) | // mask Alt + direction key seek
(Window. event. keyCode = 39) {// mask Alt + direction key →
Alert ("You are not allowed to use ALT + arrow keys to move forward or backward the webpage! ");
Event. returnvalue = false;
}
/* Note: this does not really block Alt + direction keys,
Because when the Alt + direction key pop-up warning box is displayed, press and hold the Alt key,
With the mouse clicked off the warning box, this blocking method will become invalid. If
Which of the following methods does the master really block the Alt key. */
If (event. keyCode = 8 )&&
(Event. srcElement. type! = "Text "&&
Event. srcElement. type! = "Textarea "&&
Event. srcElement. type! = "Password") | // block the backspace deletion key
(Event. keyCode = 116) | // block the F5 refresh key
(Event. ctrlKey & event. keyCode = 82) {// Ctrl + R
Event. keyCode = 0;
Event. returnvalue = false;
}
If (event. ctrlKey) & (event. keyCode = 78) // block Ctrl + n
Event. returnvalue = false;
If (event. shiftKey) & (event. keyCode = 121) // block shift + F10
Event. returnvalue = false;
If (window. event. srcElement. tagName = "A" & window. event. shiftKey)
Window. event. returnvalue = false; // block shift with the left mouse button to open a new page
If (window. event. altKey) & (window. event. keyCode = 115) {// block Alt + F4
Window. showModelessDialog ("about: blank", "", "dialogWidth: 1px; dialogheight: 1px ");
Return false ;}
}
/* In addition, you can use window. open to shield all IE menus.
Method 1:
Window. open ("Your .htm", "", "toolbar = no, location = no, directories = no, menubar = no, scrollbars = no, resizable = yes, status = no, top = 0, left = 0 ")
The second method is to open a full screen page:
Window. open ("Your. asp", "", "fullscreen = yes ")
*/
// -->
</Script>
1.4 shield the browser from the "minimize" "maximize" "Close" key in the upper right corner
<Script language = javascript>
Function window. onbeforeunload ()
{
If (event. clientX> document. body. clientWidth & event. clientY <0 | event. altKey)
{
Window. event. returnvalue = "";
}
}
</Script>
Or open the page in full screen mode.
<Script language = "javascript">
<! --
Window. open (www.32pic.com, "32pic", "fullscreen = 3, height = 100, width = 400, top = 0, left = 0, toolbar = no, menubar = no, scrollbars = no, resizable = no, location = no, status = no ");
-->
</Script>
Note: Add onbeforeunload = "javascript: return false" to the body tag to disable the window)
1.5 shield the F5 key
<Script language = "javascript">
<! --
Function document. onkeydown ()
{
If (event. keyCode = 116)
{
Event. keyCode = 0;
Event. cancelBubble = true;
Return false;
}
}
-->
</Script>
1.6 block IE back button
Use <a href = "javascript: location. replace (url)">
1.7 shield the scroll bar of the Main Window
Add style = "overflow-y: hidden" to the body tag"
1.8 shield the clipboard screen and clear the clipboard continuously
Add onload = "setInterval ('clipboarddata. setData (\ 'text \ ', \') ', 100)" to the body tag )"
1.9 shielding website Printing
<Style>
@ Media print {
* {Display: none}
}
</Style>
1.10 shield the Save icon automatically displayed on IE6.0 Images
Method 1:
<META HTTP-EQUIV = "imagetoolbar" CONTENT = "no">
Method 2:

1.11 shield all scripts on the page
<Noscrrept> </noscript>
2> form submission Verification
2.1 Form items cannot be blank
<Script language = "javascript">
<! --
Function CheckForm ()
{
If (document. form. name. value. length = 0 ){
Alert ("enter your name! ");
Document. form. name. focus ();
Return false;
}
Return true;
}
-->
</Script>
2.2 compare whether the values of the two form items are the same
<Script language = "javascript">
<! --
Function CheckForm ()
If (document. form. PWD. value! = Document. form. PWD_Again.value ){
Alert ("the password you entered twice is different! Enter again .");
Document. ADDUser. PWD. focus ();
Return false;
}
Return true;
}
-->
</Script>
2.3 form items can only be numbers and "_", used for phone/Bank Account Verification, and can be extended to domain name registration.
<Script language = "javascript">
<! --
Function isNumber (String)
{
Var Letters = "1234567890-"; // you can add input values by yourself.
Var I;
Var c;
If (String. charAt (0) = '-')
Return false;
If (String. charAt (String. length-1) = '-')
Return false;
For (I = 0; I <String. length; I ++)
{
C = String. charAt (I );
If (Letters. indexOf (c) <0)
Return false;
}
Return true;
}
Function CheckForm ()
{
If (! IsNumber (document. form. TEL. value )){
Alert ("your phone number is invalid! ");
Document. form. TEL. focus ();
Return false;
}
Return true;
}
-->
</Script>
2.4 form entry input value/length limit
<Script language = "javascript">
<! --
Function CheckForm ()
{
If (document. form. count. value> 100 | document. form. count. value <1)
{
Alert ("the input value cannot be less than zero or greater than 100! ");
Document. form. count. focus ();
Return false;
}
If (document. form. MESSAGE. value. length <10)
{
Alert ("the input text is less than 10! ");
Document. form. MESSAGE. focus ();
Return false;
}
Return true;
}
// -->
</Script>
2.5 Determination of validity of Chinese/English/numbers/email addresses
<Script language = "javascript">
<! --
Function isEnglish (name) // english value detection
{
If (name. length = 0)
Return false;
For (I = 0; I <name. length; I ++ ){
If (name. charCodeAt (I)> 128)
Return false;
}
Return true;
}
Function isChinese (name) // Chinese value detection
{
If (name. length = 0)
Return false;
For (I = 0; I <name. length; I ++ ){
If (name. charCodeAt (I)> 128)
Return true;
}
Return false;
}
Function isMail (name) // E-mail value detection
{
If (! IsEnglish (name ))
Return false;
I = name. indexOf ("@");
J = name. lastIndexOf ("@");
If (I =-1)
Return false;
If (I! = J)
Return false;
If (I = name. length)
Return false;
Return true;
}
Function isNumber (name) // value detection
{
If (name. length = 0)
Return false;
For (I = 0; I <name. length; I ++ ){
If (name. charAt (I) <"0" | name. charAt (I)> "9 ")
Return false;
}
Return true;
}
Function CheckForm ()
{
If (! IsMail (form. Email. value )){
Alert ("your email is invalid! ");
Form. Email. focus ();
Return false;
}
If (! IsEnglish (form. name. value )){
Alert ("the English name is invalid! ");
Form. name. focus ();
Return false;
}
If (! IsChinese (form. cnname. value )){
Alert ("invalid Chinese name! ");
Form. cnname. focus ();
Return false;
}
If (! IsNumber (form. PublicZipCode. value )){
Alert ("the zip code is invalid! ");
Form. PublicZipCode. focus ();
Return false;
}
Return true;
}
// -->
</SCRIPT>
2.6 restrict characters that cannot be entered for Form Items
<Script language = "javascript">
<! --
Function contain (str, charset) // string contains the test function
{
Var I;
For (I = 0; I <charset. length; I ++)
If (str. indexOf (charset. charAt (I)> = 0)
Return true;
Return false;
}
Function CheckForm ()
{
If (contain (document. form. NAME. value, "% \ (\)> <") | (contain (document. form. MESSAGE. value, "% \ (\)> <")))
{
Alert ("illegal characters entered ");
Document. form. NAME. focus ();
Return false;
}
Return true;
}
// -->
</Script>
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.