js|web| Tips
Web development Common JS functional tips
--------------------------------------------------------------------------------
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >;
<HTML>;
<HEAD>;
<TITLE>; New Document </TITLE>;
</HEAD>;
<BODY>;
<form method=post action= "" >;
<input onpaste= "return false;" type= "text" name= "TextField" style= "width:400px"; ime-mode:disabled "value=" Chinese only ">;
</FORM>;
</BODY>;
</HTML>;
-----------------------------------------------------------------------------
Only numbers can be entered
<script language= "JavaScript" >;
<!--
function Check ()
{
if (document.form.tell.value== "") {
Alert ("Please enter the phone number!") ");
Document.form.tell.focus ();
return false;
}
else{
var letters = "0123456789 () +-";
for (i=0; i< document.form.tell.value.length; i++) {
var Checkchar = document.form.tell.value.charAt (i);
if (Letters.indexof (checkchar) = =-1) {
Alert ("Phone number is not in the correct format!") ");
Document.form.tell.focus ();
return false;
}
}
}
}
-->;
</script>;
The form between <body>;...</body>;.
<form method= "POST" Name=form action= "" >;
<p align= "center" >;
Please enter the phone number: <input type= "text" name= "Tell" size= ">;"
<input type= "Submit" value= "submitted" name= "B1" >;</p>;
</form>;
--------------------------------------------------------------------------------
e mail legality check
function Check ()
{
if (document.register.email.value.length!=0)
{
if (document.register.email.value.charAt (0) = = "." | |
Document.register.email.value.charAt (0) = = "@" | |
Document.register.email.value.indexOf (' @ ', 0) = = 1 | |
Document.register.email.value.indexOf ('. ', 0) = = 1 | |
Document.register.email.value.lastIndexOf ("@")
==document.register.email.value.length-1 | |
Document.register.email.value.lastIndexOf (".")
==DOCUMENT.REGISTER.EMAIL.VALUE.LENGTH-1)
{
Alert ("The format of the email is not correct!") ");
Document.register.email.focus ();
return false;
}
}
Else
{
Alert ("Email cannot be empty!") ");
Document.register.email.focus ();
return false;
}
return false;
}
-----------------------------------------------------------
Page Right Key mask
<body oncontextmenu= "return false" >;
----------------------------------------------------------------
Automatically clear spaces in the input box
Further <script language= "JavaScript" >;
<!--//
function Ignorespaces (string) {
var temp = "";
String = ' + string;
splitstring = String.Split (""); Between double quotes is a space;
for (i = 0; i < splitstring.length; i++)
temp = splitstring;
return temp;
}
-->;
</script>;
The following code joins the <body>; area
<form method= "POST" action= "..." >;
<input type=text size=25 onblur= "this.value=ignorespaces (this.value);" Name= "text" >;
<input type=submit value= "OK" name= "button" >;
</form>;
-----------------------------------------------------------------------
Fade effect of right-click menu
<HTML>;<HEAD>;<TITLE>; Right-click Fade Effect </TITLE>;
<meta content= "text/html; charset=gb2312 "http-equiv=content-type>;
<script language=javascript>;
<!--//Rightclickmenu
var intdelay=10; Set menu display speed, bigger and slower
var intinterval=5; Transparency of each change
function Showmenuie5 () {
var rightedge=document.body.clientwidth-event.clientx
var bottomedge=document.body.clientheight-event.clienty
if (rightedge<ie5menu.offsetwidth)
Ie5menu.style.left=document.body.scrollleft+event.clientx-ie5menu.offsetwidth
Else
Ie5menu.style.left=document.body.scrollleft+event.clientx
if (bottomedge<ie5menu.offsetheight)
Ie5menu.style.top=document.body.scrolltop+event.clienty-ie5menu.offsetheight
Else
Ie5menu.style.top=document.body.scrolltop+event.clienty
ie5menu.style.visibility= "Visible"
Ie5menu.style.visibility= ""
Ie5menu.filters.alpha.opacity=0
Gradientshow ()
return False
}
function Hidemenuie5 () {
Ie5menu.style.visibility= "Hidden"
Gradientclose ()
}
function Highlightie5 () {
if (event.srcelement.classname== "MenuItems") {
Event.srcelement.style.backgroundcolor= "Highlight"
Event.srcelement.style.color= "White"
}
}
function Lowlightie5 () {
if (event.srcelement.classname== "MenuItems") {
Event.srcelement.style.backgroundcolor= ""
Event.srcelement.style.color= "#000000"
}
}
function Jumptoie5 () {
if (event.srcelement.classname== "MenuItems") {
if (Event.srcElement.url!= ') {
if (Event.srcElement.getAttribute ("target")!=null)
window.open (Event.srcelement.url,event.srcelement.getattribute ("target"))
Else
Window.location=event.srcelement.url
}
}
}
function gradientshow ()//Implementing Fading functions
{
Ie5menu.filters.alpha.opacity+=intinterval
if (ie5menu.filters.alpha.opacity<100) settimeout ("Gradientshow ()", Intdelay)
}
function Gradientclose ()//Fading functions
{
Ie5menu.filters.alpha.opacity-=intinterval
if (ie5menu.filters.alpha.opacity>;0) {
SetTimeout ("Gradientclose ()", Intdelay)
}
else {
Ie5menu.style.visibility= "Hidden"
}
}
function CHANGEBG ()//change the background color of the menu item, where the two color values can be changed to the
{
Oel= event.srcelement
if (oel.style.background!= "Navy") {
oel.style.background= "Navy"
}
Else {
oel.style.background= "#cccccc"
}
}
//-->;
</SCRIPT>;
<style type=text/css>;
. CMenu {
Filter:alpha (opacity=0); Background-color: #D6D3CE; Border-bottom: #666666 2px solid; Border-left: #E4E4E4 2px solid; Border-right: #666666 2px solid; Border-top: #E4E4E4 2px solid; COLOR: #000000; Cursor:default; font-size:9pt; Color: #000000; Font-weight:normal; line-height:20px; Position:absolute; Visibility:hidden; width:110px
}
. menuitems {
font-size:9pt;
margin:2px;
padding-bottom:0px;
padding-left:15px;
padding-right:3px;
padding-top:0px;
}
</style>;
<meta content= "Microsoft FrontPage 4.0" name=generator>;
</HEAD>;
<BODY>;
<object classid=clsid:8856f961-340a-11d0-a96b-00c04fd705a2 height=0 id=webbrowser width=0>;
</OBJECT>;
<RIGHTCLICK>;<!--[If ie]>;
<div class=cmenu id=ie5menu onclick=jumptoie5 () onmouseout=lowlightie5 ()
Onmouseover=highlightie5 ( ) >;
<div class=menuitems url= "javascript:click_obj (0)" >; back </DIV>;
<div class=menuitems url= "javascript:click_obj (1)" >; forward </DIV>;
<div class=menuhr>;<div class=menuitems url= "Javascript:click_obj (2)" >; Refresh </DIV>;
<div class=menuitems url= "Javascript:click_obj (3)" >; Add Favorites </DIV>;
<div class=menuitems url= "Javascript:click_obj (4)" >; View Source file </DIV>;
<div class=menuhr>;<div class=menuitems url= "Javascript:click_obj (5)" >; Properties </DIV>;
</DIV>;
<! [endif]-->;
<script language=javascript>;
<!--
function Click_obj (ID) {
Switch (ID) {
Case 0:
History.back ()
Break
Case 1:
History.forward ()
Break
Case 2:
Window.location.reload ()
Break
Case 3:
Window.external.AddFavorite (Location.href, Document.title)
Break
Case 4:
window.location = "View-source:" + window.location.href
Break
Case 5:
Document.all.WebBrowser.ExecWB (10,1)
Break
}
}
if (document.all&&window.print) {
Ie5menu.classname= "CMenu"
Document.oncontextmenu=showmenuie5
Document.body.onclick=hidemenuie5
}
-->;
</SCRIPT>;
</RIGHTCLICK>;
</BODY>;</HTML>;
----------------------------------------------------------------------------
IE with ActiveX implementation printing I have not tested the version, IE6 under normal
<script language=javascript>;
<!--
function Setprint ()
{
WB. EXECWB (8,1);
}
function Previewprint ()
{
WB. EXECWB (7,1)
}
-->;
</SCRIPT>;
In <body>;...</body>; add:
<object classid=clsid:8856f961-340a-11d0-a96b-00c04fd705a2 height=0 ID=WB width=0>;
</OBJECT>;
<input type= "button" value= "Set" Id=button1 name=button1 >;
<input type= "button" value= "Preview" Id=button2 name=button2 >;
----------------------------------------------------------------
Hyperlink Submit form
<script LANGUAGE = "JavaScript" >;
Function CheckValue ()
{
if (document.welcomeform.nickname.value== ")
{
Alert (" Nickname cannot be empty! ");
return (false);
}
if (document.welcomeform.password.value== ")
{
alert (" Password cannot be empty! ") ");
Return (false);
}
document.welcomeform.submit ();
return (True);
}
</script>;
Add the following code between <body>;...</body>;:
<form name= "Welcomeform" method= "post" action= "welcome.asp" >;
<a href= "#" >; login </a>;
</form>;
----------------------------------------------------------
Caching pages is not allowed
Using Java-provided methods, either in a JSP or in a servlet
<%
Response.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expires", 0);
%>;
----------------------------------------