📄 scw.js
字号:
// *****************************************************************************// Simple Calendar Widget - Cross-Browser Javascript pop-up calendar.//// Copyright (C) 2005-2006 Anthony Garrett//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, it is available at// the GNU web site (http://www.gnu.org/) or by writing to the// Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,// Boston, MA 02110-1301 USA//// *****************************************************************************//// Contact: Sorry, I can't offer support for this but if you find a problem// (or just want to tell me how useful you find it), please send// me an email at scwfeedback@tarrget.info (Note the two Rs in// tarrget). I will try to fix problems quickly but this is a// spare time thing for me.//// Credits: I wrote this from scratch myself but I couldn't have done it// without the superb "JavaScript The Definitive Guide" by David// Flanagan (Pub. O'Reilly ISBN 0-596-00048-0). I also recognise// a contribution from my experience with PopCalendar 4.1 by// Liming(Victor) Weng.//// Link back: Please give me credit and link back to my page. To ensure that// search engines give my page a higher ranking you can add the// following HTML to any indexed page on your web site://// <A HREF="http://www.tarrget.info/calendar/scw.htm">// Simple Calendar Widget by Anthony Garrett// </A>//// Features: Easily customised// (output date format, colours, language, year range and// week start day)// Accepts a date as input// (see comments below for formats).// Cross-browser code tested against;// Internet Explorer 6.0.28 Mozilla 1.7.1// Opera 7.52+ Firefox 0.9.1+// Konqueror 3.4.0 Flock 0.4.9//// How to add the Calendar to your page:// This script needs to be defined for your page so, immediately// after the BODY tag add the following line;//// <script type='Text/JavaScript' src='scw.js'></script>//// Your root directory of the web site should also contain an empty// file called "scwblank.html". See// http://www.tarrget.info/calendar/IEnightmare.html// for a full explanation.//// How to use the Calendar once it is defined for your page://// Simply choose an event to trigger the calendar (like an onClick// or an onMouseOver) and an element to work on (for the calendar// to take its initial date from and write its output date to) then// write it like this;//// <<event>>="scwShow(<<element>>,this);"//// e.g. onClick="scwShow(scwID('myElement'),this);"// or onMouseOver="scwShow(this,this);"//// NOTE: The "this" keyword causes the script to fail when the// triggered calendar is using an anchor tag. The following// syntax works;//// <a id="<<ID>>"// href="javascript:// scwShow(<<element>>,scwID('<<ID>>'));">// <<your text>>// </a>//// If you are using a text node then specify the text's parent node// in the function call. The date should be the only text under that// node;//// e.g. <p onclick="scwShow(this,this);"><<date>></p>//// You can also disable days of the week by adding arguments to the// call to scwShow. The values should be Sunday = 0 through to// Saturday = 6. A call to scwShow with Friday and Monday disabled// would look something like this;//// scwShow(<<element>>,this,5,1);//// Finally you can use the following technique to run a function// when the calendar closes://// scwNextAction=<<function>>.runsAfterSCW(this,<<arguments>>);// scwShow(<<element>>,this <<,optional arguments above>>);//// Where <<function>> is a function defined on the calling page// and <<arguments>> is the list of arguments being passed to that// function.//// ------------------------------------------------------------------// Here's an extremely trivial but fully functioning example page// showing two of the ways to trigger the calendar;//// <html>// <head><title>Basic Example</title></head>// <body>// <script type='text/JavaScript' src='scw.js'></script>// <p onclick='scwShow(this,this);'>06-Dec-2006</p>// <input onclick='scwShow(this,this);' value='07-Dec-2006' />// <br/><br/>// <a id='aTest'// href='javascript:scwShow(scwID("aTest"),this);'>// 08-Dec-2006// </a>// </body>// </html>//// *****************************************************************************//// See http://www.tarrget.info/calendar/scw.htm for a complete version history//// Version Date By Description// ======= ==== =============== ===========// 3.56 2006-12-13 Anthony Garrett Fixed accidental disabling of the last// day of the last month in the calendar.// Added options to disable and hide days// that are out of the currently displayed// month. Thanks to Ruslan Androsyuk for// this feature request.// Applied the IFRAME backing to IE 7// in order to improve rendering when// the calendar overlays graphically// dynamic objects. Thanks to Shaun// Plumb for reporting this issue.// Allowed date input/output to be a// text node. Thanks to John Navratil// for this suggestion (and others).// Applied a work-around for Opera's// buggy selectIndex setting.// *****************************************************************************// ************************************// Start of Simple Calendar Widget Code// ************************************// This date is used throughout to determine today's date.var scwDateNow = new Date(Date.parse(new Date().toDateString()));//******************************************************************************//------------------------------------------------------------------------------// Customisation section//------------------------------------------------------------------------------//******************************************************************************// Set the bounds for the calendar here...// If you want the year to roll forward you can use something like this...// var scwBaseYear = scwDateNow.getFullYear()-5;// alternatively, hard code a date like this...// var scwBaseYear = 1990;var scwBaseYear = scwDateNow.getFullYear() - 10;// How many years do want to be valid and to show in the drop-down list?var scwDropDownYears = 20;// All language-dependent changes can be made here...// If you wish to work in a single language (other than English) then// just replace the English (in the function scwSetLanguage below) with// your own text.// Using multiple languages:// In order to keep this script to a resonable size I have not included// languages here. You can set language fields in a function that you// should call scwSetLanguage the script will use your languages.// I have included all the translations that have been sent to me in// such a function on the demonstration page.var scwLanguage;function scwSetDefaultLanguage(){ try { scwSetLanguage(); } catch (exception) {// English scwToday = 'Today:'; scwDrag = 'click here to drag'; scwArrMonthNames = ['Jan','Feb','Mar','Apr','May','Jun', 'Jul','Aug','Sep','Oct','Nov','Dec']; scwArrWeekInits = ['S','M','T','W','T','F','S']; scwInvalidDateMsg = 'The entered date is invalid.\n'; scwOutOfRangeMsg = 'The entered date is out of range.'; scwDoesNotExistMsg = 'The entered date does not exist.'; scwInvalidAlert = ['Invalid date (',') ignored.']; scwDateDisablingError = ['Error ',' is not a Date object.']; scwRangeDisablingError = ['Error ', ' should consist of two elements.']; }}// Note: Always start the scwArrWeekInits array with your string for// Sunday whatever scwWeekStart (below) is set to.// scwWeekStart determines the start of the week in the display// Set it to: 0 (Zero) for Sunday, 1 (One) for Monday etc..var scwWeekStart = 1;// The week start day for the display is taken as the week start// for week numbering. This ensures that only one week number// applies to one line of the calendar table.// [ISO 8601 begins the week with Day 1 = Monday.]// If you want to see week numbering on the calendar, set// this to true. If not, false.var scwWeekNumberDisplay = false;// Week numbering rules are generally based on a day in the week// that determines the first week of the year. ISO 8601 uses// Thursday (day four when Sunday is day zero). You can alter// the base day here.// See http://www.cl.cam.ac.uk/~mgk25/iso-time.html for more informationvar scwWeekNumberBaseDay = 4;// Each of the calendar's alert message types can be disabled// independently here.var scwShowInvalidDateMsg = true, scwShowOutOfRangeMsg = true, scwShowDoesNotExistMsg = true, scwShowInvalidAlert = true, scwShowDateDisablingError = true, scwShowRangeDisablingError = true;// Set the allowed input date delimiters here...// E.g. To set the rising slash, hyphen, full-stop (aka stop or point),// comma and space as delimiters use// var scwArrDelimiters = ['/','-','.',',',' '];var scwArrDelimiters = ['/','-','.',',',' '];// Set the format for the displayed 'Today' date and for the output// date here.//// The format is described using delimiters of your choice (as set// in scwArrDelimiters above) and case insensitive letters D, M and Y.//// Definition Returns// ---------- -------// D date in the month without zero filling// DD date in the month left zero filled// M month number without zero filling// MM month number left zero filled// MMM month string from scwArrMonthNames// YY year number in two digits// YYYY year number in four digits// Displayed "Today" date formatvar scwDateDisplayFormat = 'dd-mm-yy';// e.g. 'MMM-DD-YYYY' for the US// Output date formatvar scwDateOutputFormat = 'DD MMM, YYYY';// e.g. 'MMM-DD-YYYY' for the US// The input date is fully parsed so a format is not required,// but there is no way to differentiate the sequence reliably.//// e.g. Is 05/08/03 5th August 2003,// 8th May 2003 or even// 3rd August 2005?//// So, you have to state how the code should interpret input dates.//// The sequence should always contain one D, one M and one Y only,// in any order.var scwDateInputSequence = 'DMY';// e.g. 'MDY' for the US// Note: Because the user may select a date then trigger the// calendar again to select another, it is necessary to// have the input date sequence in the same order as the// output display format. To allow the flexibility of having// a full input date and a partial (e.g. only Month and Year)// output, the input sequence is set separately.//// The same reason determines that the delimiters used should// be in scwArrDelimiters.// scwZindex controls how the pop-up calendar interacts with the rest// of the page. It is usually adequate to leave it as 1 (One) but I// have made it available here to help anyone who needs to alter the// level in order to ensure that the calendar displays correctly in// relation to all other elements on the page.var scwZindex = 1;// Personally I like the fact that entering 31-Sep-2005 displays// 1-Oct-2005, however you may want that to be an error. If so,// set scwBlnStrict = true. That will cause an error message to// display and the selected month is displayed without a selected// day. Thanks to Brad Allan for his feedback prompting this feature.var scwBlnStrict = false;// If you wish to disable any displayed day, e.g. Every Monday,// you can do it by setting the following array. The array elements// match the displayed cells.//// You could put something like the following in your calling page// to disable all weekend days;//// for (var i=0;i<scwEnabledDay.length;i++)// {if (i%7%6==0) scwEnabledDay[i] = false;}//// The above approach will allow you to disable days of the week// for the whole of your page easily. If you need to set different// disabled days for a number of date input fields on your page// there is an easier way: You can pass additional arguments to// scwShow. The syntax is described at the top of this script in// the section:// "How to use the Calendar once it is defined for your page:"//// It is possible to use these two approaches in combination.var scwEnabledDay = [true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -