Common JavaScript code

Source: Internet
Author: User
Tags rtrim saveform

// Close the window event
Function closeWindow (){
If (event. clientX> document. body. clientWidth ){
Event. returnValue = "are you sure you want to exit the system? ";
} Else {
Return;
}
}
Window. onunload = function (){
If (event. clientX> document. body. clientWidth ){
RemoveChat ();
Form1.action = "login. do? Action = logout ";
Form1.submit ();
} Else {
Return;
}
};

========================================================== ======================================

// Disable the F5 key code
Function document. onkeydown (){
If (window. event. keyCode = 116 ){
Window. event. keyCode = 0;
Window. event. returnValue = false;
}
}

========================================================== ======================================

// Open a new window

Window. open ("www.baidu.com", "Baidu Homepage", "height = 450, width = 800, status = yes, toolbar = no, menubar = no, location = no ");

========================================================== ======================================

// Obtain the element of the iframe subpage from the parent page

Registry.frames{'mainframe'{.doc ument. getElementById ("noticeDiv ")

========================================================== ======================================

// Judge an empty string
String. prototype. isEmpty = function () {return/^/s * $/. test (this );}

========================================================== ======================================

// Page Jump

Window. location = "www.baidu.com ";

Window. location. href = "www.baidu.com ";

========================================================== ======================================

// UncheckAll cancel check box
Function uncheckAll (form, checkFlag ){
Var formVar = document. getElementById (form );
Var x = document. getElementsByTagName ("INPUT ");
Var checkAllFlag = true;
If (! CheckFlag ){
FormVar. chkAll. checked = false;
}
Else {
For (k = 0; k If (x [k]. value! = "ChkAll" & x [k]. type = "checkbox" & x [k]. checked = false)
CheckAllFlag = false;
}
FormVar. chkAll. checked = checkAllFlag;
}
}
// Select all check boxes
Function CheckAll (form ){
Var formVar = document. getElementById (form );
Var x = document. getElementsByTagName ("INPUT ");
For (k = 0; k If (x [k]. type = "checkbox "){
X [k]. checked = formVar. chkAll. checked;
}
}
}

========================================================== ======================================

/* Obtain the current system time */
Var today = new Date ();
Var month = today. getMonth () + 1;
Var now = today. getFullYear () + "-" + month + "-" + today. getDate () + "00:00:00 ";

========================================================== ======================================

// The start time cannot be later than the end time or earlier than the current system time
Function validBeginAndEnd (wrongTime ){
Var beginTime = obj ("beginTime"). value;
Var endTime = obj ("endTime"). value;
BeginTime = beginTime. replace (//-/g ,"//");
EndTime = endTime. replace (//-/g ,"//");
Now = now. replace (//-/g ,"//");

If (new Date (beginTime) <new Date (now )){
Alert ("the start time cannot be earlier than the current system time! ");
WrongTime. value = "";
Return false;
}
If (beginTime! = "" & EndTime! = ""){
If (new Date (beginTime)> new Date (endTime ))){
Alert ("the start time cannot be later than the end time! ");
WrongTime. value = "";
Return false;
}
}
}

========================================================== ======================================

// Go to the modification page
Function toModifyAuth (form ){
Var formVar = document. getElementById (form );
Var x = document. getElementsByTagName ("INPUT ");
Var I = 0;
Var authId = null;
For (k = 0; k If (x [k]. value! = "ChkAll" & x [k]. type = "checkbox" & x [k]. checked = true ){
If (x [k]. editable = "F "){
Var msg = "You are not authorized to modify" + x [k]. authName + ". Contact the administrator! ";
Alert (msg );
Return;
}
I ++;
AuthId = x [k]. value;
}
}
If (I = 1 ){
FormVar. action = "auth. do? Action = toModifyAuth & authId = "+ authId;
FormVar. submit ();
}
If (I <1 ){
Alert ("Check the record you want to modify! ");
}
If (I> 1 ){
Alert ("only one record can be selected! ");
}
}

========================================================== ======================================

// Verify the contact number
Var patrn =/^ [0-9] {1, 20} $ /;
Var linkTel = obj ("linkTel"). value;
If (patrn.exe c (linkTel) = null ){
Alert ("the contact number is composed of numbers. Please enter a valid phone number! ");
Obj ("linkTel"). focus ();
Obj ("linkTel"). select ();
Return;
}

========================================================== ======================================

// Pure digit Verification

Onkeyup = "this. value = this. value. replace (// D/g ,'')"
Onafterpaste = "this. value = this. value. replace (// D/g ,'')"

========================================================== ======================================

// The Child page calls the JavaScript method of the parent page

Window. parent. showContent ();

========================================================== ======================================

// Obtain the Form Element saveForm of the child page from the parent page and submit the data of this Form.

Too many frames['templateframe').doc ument. getElementById ("saveForm"). action = "login. jsp ";
Parameter parameter frames['templateframe').doc ument. getElementById ("saveForm"). submit ();

========================================================== ==========================================

// File Upload

<% @ Page language = "java" contentType = "text/html; charset = GBK" pageEncoding = "GBK" %>
<% @ Include file = "../common/incHead. jsp" %>




Online editing template Import


<Script language = "JavaScript" src = "$ {ctx}/js/formValidate. js"> </script>
<Script type = "text/javascript" src = "$ {ctx}/js/commonUtils. js"> </script>
<Script type = "text/javascript">
// Template Import
Function importTemplate (){
If (checkForm (document. all. fileForm) = true ){
Var x = document. getElementsByTagName ("input ");
For (k = 0; k If (x [k]. type = "file "){
Var exp = x [k]. value. substring (x [k]. value. lastIndexOf ("/.") + 1). toLowerCase ();
If (exp! = "Htm" & exp! = "Html "){
Alert ("only webpage files can be edited! ");
Return;
}
}
}
Document. all. fileForm. action = "editorTemplate. do? Action = importTemplate & operate = copy ";
FileForm. submit ();
}
}

// File Upload
Function addfile (){
Var len = document. all. fileTable. rows. length;
Var row = document. all. fileTable. insertRow (len );
Var cell1 = row. insertCell ();
If (window. file_ I = undefined ){
Window. file_ I = 1;
}
Cell1.innerHTML =" "+
" ";
}

// Delete a file
Function removeFile (srcObj ){
Var currRowIndex = srcObj. parentNode. parentNode. rowIndex;
Document. all. fileTable. deleteRow (currRowIndex );
}
</Script>





Upload Template





Custom template






========================================================== ========================================================

// Pure digit Verification

Function isNumber (oNum)
{
If (! ONum) return false;
Var strP =/^/d + (/./d + )? $ /;
If (! StrP. test (oNum) return false;
Try {
If (parseFloat (oNum )! = ONum) return false;
}
Catch (ex)
{
Return false;
}
Return true;
}

========================================================== ==============================================

// Phone number verification

Function f_check_phoneOrMobile (obj)
{
Var regu =/(^ ([0] [1-9] {2, 3} [-])? /D {3, 8} (-/d {1, 6 })? $) | (^/([0] [1-9] {2, 3}/)/d {3, 8} (/d {1, 6 }/))? $) | (^/D {3, 8} $ )/;
Var regu2 =/(^ [1] [3-5] [0-9] {9} $) | (^ 0 [1] [3-5] [0-9] {9} $ )/;
Var re = new RegExp (regu );
Var re2 = new RegExp (regu2 );
If (re. test (obj. value) | re2.test (obj. value )){
Return true;
}
Alert ("the contact number must be a number ");
Return false;
}

========================================================== ==============================================

Triggered after page elements are loaded

Document. getElementById ("message"). onreadystatechange = function (){
If (this. readyState = "complete "){
DocumentOf ("message"). body. innerHTML = date [index-1]. CONTENT;
}
}

========================================================== ==============================================

// Operate the linknam element of the parent form on the pop-up window page

Zookeeper opener.doc ument. getElementById ("linknam"). value = staffName;

========================================================== ==============================================

Frames ["message" cmd.doc ument. designMode = "On ";

Obj ("content"). value = frames ["message" example .doc ument. body. innerHTML;

========================================================== ==============================================

Processing by space in JS:

String. prototype. Trim = function () {return this. replace (/^/s + |/s + $/g ,"");}

String. prototype. Ltrim = function () {return this. replace (/^/s +/g ,"");}

String. prototype. Rtrim = function () {return this. replace (// s + $/g ,"");}

Var str = "helloworld ";

Alert ("----" + str. Ltrim () + "-----"); // remove the left space

Alert ("----" + str. Rtrim () + "-----"); // remove the right space

Alert ("----" + str. Trim () + "-----"); // spaces are removed from both sides.

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.