JavaScript usage tips (. NET Html_javascript Tips

Source: Internet
Author: User
Tags delete key domain name registration to domain
(i). Confirm Deletion usage:
1. BTNDEL.ATTRIBUTES.ADD ("onclick", "Return Confirm" ("+" confirm deletion?) +"')");
2. linktempdelete.attributes["onclick"]= "Javascript:return confirm (' +" confirm deletion?) +"');";
3. private void Grdproject_itemdatabound (object sender, DataGridItemEventArgs e)
4.
{
if ((E.item.itemtype = = ListItemType.Item) | (E.item.itemtype = = ListItemType.AlternatingItem))
{
Delete the hint section on the button
E.ITEM.CELLS[10]. Attributes.Add ("onclick", "return Confirm" (' OK delete?) ');");
}
}
5.
<script language= "JavaScript" type= "Text/javascript" >
function Delete_y (e)
{
if (Event.srcElement.outerText = = "Delete")
Event.returnvalue=confirm ("Confirm deletion?");
}
document.onclick=delete_y;
</script>
(ii). Cross-language string substitution
System.Text.RegularExpressions.Regex.Replace (str1,@ "<{1}[^ <>]*>{1}", ""). Replace ("", ""). Replace ("\", "\ \"). Replace ("-", "-"). Replace ("&", "&"). Replace ("<", "<"). Replace (">", ">"). Replace ("br", "\ n");
(iii). Close form
1.
THIS.BTNCLOSE.ATTRIBUTES.ADD ("onclick", "window.close (); return false;");
2. Turn off this form to 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 this pop-up form and refresh the parent page
This. Response.Write ("<script language= ' JavaScript" >window.opener.location.reload (); Window.close (); </script> ");
(iv). Web MessageBox
1.
Response.Write ("<script language=javascript> window.alert" (' Save success 1 '); </script> ");
2.
Response.Write ("<script>alert" + "Save success!") +"'); </script> ");
3.
This. Page.registerstartupscript ("Chiname", "<script language= ' JavaScript ' >alert (' + ' save success!" + ") </script>") ;
(v). Add a CheckBox control template column to the DataGrid.
Please see: http://blog.csdn.net/chengking/archive/2005/10/08/497520.aspx
(vi). window.open () method
Grammar: window.open (pageurl,name,parameters);
The open () method of the Window object is used to create a new window instance, and 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 based on its parameters: name-determined names.
The following table is the Parameters parameter table:
Parameter Panties Start Value description
Alwayslowered yes/no Specifies that the window is hidden beneath all windows.
Alwaysraised yes/no specifies that the window floats above all windows.
Dependent yes/no Specifies that the open window is a window of the parent window. and closes with the parent window closed.
Directions YES/NO Specifies whether Navigator 2 and 3 are visible in the new window.
Height pixel value sets the pixel height of the new window.
Hotkeys yes/no to set a secure exit hotkey in a new window without a menu bar.
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 position bar is visible in the new window.
MenuBar yes/no Indicates whether the menu bar is visible in the new window.
The pixel height of the Outerheight Pixel Value setting window (including the trim border).
Outerwidth pixel value Sets the pixel width of the window (including the trim 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 sets the pixel length of the new window from the edge of the screen.
ScrollBars yes/no Indicates whether the scroll bar is visible in the new window.
TitleBar yes/no Indicates whether the menu topic bar is visible in the new window.
Toolbar yes/no Indicates whether the toolbar is visible in the new window.
Width pixel value Sets the pixel widths of the window.
Z-look yes/no contains an array of each <pplet> label in the document.
Fullscreen yes/no whether the open form is displayed in full screen
Left pixel value sets the distance of the new window from the left side of the screen
Top pixel value sets the distance of the new window from above the screen
Example:
<title>window.open function </title>
<body>
<script language= "JavaScript" >
<!--
window.open ("", "name1", "width=100,height=200,toolbar=no,scrollbars=no,menubar=no,screenx=100,screeny=100");
-->
</script>
</body>
(vii). Location objects
href entire URL string.
Protocol A string containing the first part of the URL, such as http:
The host contains a string with the host name: part of the port number in the URL. such as//www.cenpok.net/server/
Hostname A string containing the host name in the URL. such as Http://www.cenpok.net
Port contains a string of port numbers that may exist in the URL.
Pathname after the "/" in the URL. such as ~list/index.htm
The string after the hash "#" (the CGI parameter).
Search "?" String after the number (CGI parameter).
(eight). Button capture
1.ctrl+enter Button 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 Speed Czech key: Alt+a
<button accesskey=a title= "alt+a" onclick= "alert (' button clicked! ')" >alt+a </button>
(ix). Control input, illegal characters cannot be entered into the TextBox.
<asp:textbox class= "Text"
Onkeypress= "if (Event.keycode < | | | Event.keycode >57) Event.returnvalue = false;"
Id= "Txty_revenue" style= "Text-align:right" runat= "Server" width= "90%" maxlength= ">"
</asp:textbox>
Note: This method controls the textbox only to receive the number: 0~9, also can define other input characters, such as: 65~123, only allow input: A~z and a~z.
[Below for collection]
1> Shielding function Class
1.1 Screen Keyboard all keys
<script language= "JavaScript" >
<!--
function Document.onkeydown () {
Event.keycode = 0;
Event.returnvalue = false;
}
-->
</script>
1.2 Screen right mouse button
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 Mask CTRL + N, SHIFT+F10, F5 refresh, backspace key
<script language= "JavaScript" >
<!--
Screen the right mouse button, CTRL + N, SHIFT+F10, F5 refresh, backspace key
function Window.onhelp () {return false}//Shield F1 Help
function KeyDown () {
if ((Window.event.altKey) &&
((window.event.keycode==37) | |//Shielding ALT + Direction key ←
(window.event.keycode==39))) {//Shielding ALT + Direction key →
Alert ("You are not allowed to use ALT + arrow keys forward or back page!") ");
Event.returnvalue=false;
}
* Note: This is not really shielding ALT + arrow keys,
Because ALT + ARROW keys pop-up warning box, hold down the ALT key,
With the mouse point off the warning box, this shielding method is invalid. Later if
There is a master who has really shielded the Alt key method, please inform. */
if ((Event.keycode = 8) &&
(Event.srcElement.type!= "text" &&
Event.srcElement.type!= "textarea" &&
Event.srcElement.type!= "password") | | Mask BACKSPACE Delete key
(event.keycode==116) | | Mask F5 Refresh Key
(Event.ctrlkey && event.keycode==82)) {//ctrl + R
event.keycode=0;
Event.returnvalue=false;
}
if ((Event.ctrlkey) && (event.keycode==78))//Shield CTRL + N
Event.returnvalue=false;
if ((Event.shiftkey) && (event.keycode==121))//Shielding SHIFT+F10
Event.returnvalue=false;
if (Window.event.srcElement.tagName = "A" && Window.event.shiftKey)
Window.event.returnvalue = false; Screen shift plus left mouse button new page open
if ((Window.event.altKey) && (window.event.keycode==115)) {//Shield ALT+F4
Window.showmodelessdialog ("About:blank", "" "," dialogwidth:1px;dialogheight:1px ");
return false;}
}
/* can also be used to screen all the menu of IE window.open
The first method:
window.open ("Your. htm", "", "toolbar=no,location=no,directories=no,menubar=no,scrollbars=no,resizable=yes,status= No,top=0,left=0 ")
The second way is to open a Full-screen page:
window.open ("Your. asp", "", "Fullscreen=yes")
*/
-->
</script>
1.4 Screen browser in the upper right corner, minimize, maximize, close key
<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 with full screen
<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 (so that the window cannot be closed)
1.5 Shielding F5 Key
<script language= "JavaScript" >
<!--
function Document.onkeydown ()
{
if (event.keycode==116)
{
Event.keycode = 0;
Event.cancelbubble = true;
return false;
}
}
-->
</script>
1.6 Screen IE Back button
Use <a href= "javascript:location.replace (URL)" When you link to >
1.7 Screen main Window scroll bar
Add style= "Overflow-y:hidden" to the body tag.
1.8 screen screen, constantly emptying the clipboard
Add onload= to the body tag "setinterval" (' Clipboarddata.setdata (\ ' text\ ', \ ' \ ') ', 100) '
1.9 Screen printing function of the website
<style>
@media Print {
* {Display:none}
}
</style>
1.10 Screen IE6.0 Save icon automatically appears on the picture
Method One:
<meta http-equiv= "Imagetoolbar" content= "no" >
Method Two:

1.11 All script in the screen page
<noscrript> </noscript>
2> form Submission Validation class
2.1 Form items cannot be empty
<script language= "JavaScript" >
<!--
function Checkform ()
{
if (Document.form.name.value.length = = 0) {
Alert ("Please enter your name!");
Document.form.name.focus ();
return false;
}
return true;
}
-->
</script>
2.2 Compare the values of two table items
<script language= "JavaScript" >
<!--
function Checkform ()
if (Document.form.PWD.value!= document.form.PWD_Again.value) {
Alert ("The password you entered two times is different!") Please re-enter. ");
Document. ADDUser.PWD.focus ();
return false;
}
return true;
}
-->
</script>
2.3 Form items can only be numbers and "_" for phone/bank account verification, can be extended to domain name registration, etc.
<script language= "JavaScript" >
<!--
function Isnumber (String)
{
var letters = "1234567890-"; You can add your own input value
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 not legal!") ");
Document.form.TEL.focus ();
return false;
}
return true;
}
-->
</script>
2.4 Table Single input value/length limit
<script language= "JavaScript" >
<!--
function Checkform ()
{
if (Document.form.count.value > | | Document.form.count.value < 1)
{
Alert ("The input value cannot be less than 0 or greater than 100!");
Document.form.count.focus ();
return false;
}
if (Document.form.MESSAGE.value.length <10)
{
Alert ("Input text is less than 10!");
Document.form.MESSAGE.focus ();
return false;
}
return true;
}
-->
</script>
2.5 Chinese/English/digital/email address legality judgment
<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)//numeric 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 illegal!) ");
Form. Email.focus ();
return false;
}
if (! isenglish (Form.name.value)) {
Alert ("The English name is illegal!") ");
Form.name.focus ();
return false;
}
if (! Ischinese (Form.cnname.value)) {
Alert ("Chinese name is illegal!") ");
Form.cnname.focus ();
return false;
}
if (! isnumber (Form). Publiczipcode.value)) {
Alert ("ZIP code not valid!) ");
Form. Publiczipcode.focus ();
return false;
}
return true;
}
-->
</SCRIPT>
2.6 Qualifying A form item cannot enter characters
<script language= "JavaScript" >
<!--
function contain (Str,charset)//string contains test functions
{
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 ("entered an illegal character");
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.