📄 utils.js
字号:
/* * CyberTester - J2EE Web application for creating, delivering and managing tests/exams/surveys. * Copyright (C) 2003 CyberDemia Research and Services Pty Ltd * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program (see the file COPYING); if not, write to the * Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * See the COPYING file located in the top-level-directory of * the archive of this program for complete text of license. *//*Provides cross-browser interface for basic functions. Browser differences are handled here.*/var bIE = (document.all) ? true : false;var bOPERA = (navigator.userAgent.toLowerCase().indexOf('opera')!=-1);/*Gets the width of the document in the specified window object.*/function getDocWidth(theWin) { if (bIE) { return theWin.document.body.clientWidth; } return theWin.innerWidth;}/*Gets the height of the document in the specified window object.*/function getDocHeight(theWin) { if (bIE) { return theWin.document.body.clientHeight; } return theWin.innerHeight;}/*Gets X scroll position of specified window*/function getScrollX(theWin) { if (bIE) { return theWin.document.body.scrollLeft; } return theWin.scrollX;}/*Gets Y scroll position of specified window*/function getScrollY(theWin) { if (bIE) { return theWin.document.body.scrollTop; } return theWin.scrollY;}/*Sets the left, top, width and height style properties of the elementwith specified ID in the specified window.The properties must be specified as integers (pixels).To leave out a particular property, just pass in "" (or any non-integer String).For example: To set left & top properties and leave out width & height : setElemByIdBounds( window.self, "button1", 200, 50, "", "");*/function setElemByIdBounds( theWin, id, posLeft, posTop, width, height ) { setElemBounds( theWin.document.getElementById( id ), posLeft, posTop, width, height );}/*Sets the left, top, width and height style properties of thespecified element object.The properties must be specified as integers (pixels).To leave out a particular property, just pass in "" (or any non-integer String).For example: To set left & top properties and leave out width & height : setElemBounds( button1Ref, 200, 50, "", "");*/function setElemBounds( elem, posLeft, posTop, width, height ) { if (elem) { if (elem.style) { if (bIE || bOPERA) { if (!isNaN(parseInt(posTop))) { elem.style.pixelTop = posTop; } if (!isNaN(parseInt(posLeft))) { elem.style.pixelLeft = posLeft; } if (!isNaN(parseInt(width))) { elem.style.pixelWidth = width; } if (!isNaN(parseInt(height))) { elem.style.pixelHeight = height; } } else { if (!isNaN(parseInt(posTop))) { elem.style.top = posTop+'px'; } if (!isNaN(parseInt(posLeft))) { elem.style.left = posLeft+'px'; } if (!isNaN(parseInt(width))) { elem.style.width = width+'px'; } if (!isNaN(parseInt(height))) { elem.style.height = height+'px'; } } } }} /*Gets the top coordinate (pixels) of the elementwith specified ID in the specified window.*/function getElemByIdTop( theWin, id ) { return getElemTop( theWin.document.getElementById( id ) );}/*Gets the top coordinate (pixels) of the specified element object.*/function getElemTop( elem ) { if (elem) { if (elem.style) { if (bIE || bOPERA) { return elem.style.pixelTop; } else { var topInt = parseInt(elem.style.top); if (isNaN(topInt)) { topInt = 0; } return topInt; } } } return 0;}/*Gets the left coordinate (pixels) of the elementwith specified ID in the specified window.*/function getElemByIdLeft( theWin, id ) { return getElemLeft( theWin.document.getElementById( id ) );}/*Gets the left coordinate (pixels) of the specified element object.*/function getElemLeft( elem ) { if (elem) { if (elem.style) { if (bIE|| bOPERA) { return elem.style.pixelLeft; } else { var leftInt = parseInt(elem.style.left); if (isNaN(leftInt)) { leftInt = 0; } return leftInt; } } } return 0;}/*Gets the width (pixels) of the elementwith specified ID in the specified window.*/function getElemByIdWidth( theWin, id ) { return getElemWidth( theWin.document.getElementById( id ) );}/*Gets the width (pixels) of the specified element object.*/function getElemWidth( elem ) { if (elem) { if (elem.style) { if (bIE|| bOPERA) { return elem.style.pixelWidth; } else { var wInt = parseInt(elem.style.width); if (isNaN(wInt)) { wInt = 0; } return wInt; } } } return 0;}/*Gets the height (pixels) of the elementwith specified ID in the specified window.*/function getElemByIdHeight( theWin, id ) { return getElemHeight( theWin.document.getElementById( id ) );}/*Gets the height (pixels) of the specified element object.*/function getElemHeight( elem ) { if (elem) { if (elem.style) { if (bIE|| bOPERA) { return elem.style.pixelHeight; } else { var hInt = parseInt(elem.style.height); if (isNaN(hInt)) { hInt = 0; } return hInt; } } } return 0;}/*Returns true if val is a number and min <= val <= maxOtherwise returns false.*/function isIntegerInRange( val, min, max ) { if (isNaN(val)) { return false; } if (val < min) { return false; } if (val > max) { return false; } return true;}/* Gets the target element of an event. The target is always the element that the event originally happens on.*/function getEventTarget( evt ) { var eTgt = null; if ( evt.target ) { eTgt = evt.target; } else if ( evt.srcElement ) { eTgt = evt.srcElement; } return eTgt;}/* Gets the x screen position of a window.*/function getWindowScreenXPos( win ) { if (!isNaN(win.screenX)) { return win.screenX; } return win.screenLeft;}/* Gets the y screen position of a window.*/function getWindowScreenYPos( win ) { if (!isNaN(win.screenY)) { return win.screenY; } return win.screenTop;}/* Gets X offset of an element from the document's left edge.*/function getXOffsetFromDocument( elem ) { if ( elem==document) { return 0; } var offset = elem.offsetLeft; var parent = elem.offsetParent; while ((parent!=null) && (parent!=document)) { offset = offset + parent.offsetLeft; parent = parent.offsetParent; } return offset;}/* Gets Y offset of an element from the document's top edge.*/function getYOffsetFromDocument( elem ) { if ( elem==document) { return 0; } var offset = elem.offsetTop; var parent = elem.offsetParent; while ((parent!=null) && (parent!=document)) { offset = offset + parent.offsetTop; parent = parent.offsetParent; } return offset;}/* Checks if specified element contains coordinates xpos,ypos relative to the top-left corner of the document.*/function isElemContainsPoint( elem, xpos, ypos ) { var elemX = getXOffsetFromDocument(elem); var elemY = getYOffsetFromDocument(elem); if ( (xpos<elemX) || (xpos>(elemX+elem.offsetWidth))) { return false; } if ( (ypos<elemY) || (ypos>(elemY+elem.offsetHeight))) { return false; } return true;}/*Truncates a string if it is longer than specified maxlen.*/function truncateString(str, maxlen) { if (str.length>maxlen) { if (maxlen>3) { return str.substr(0, maxlen-3)+"..."; } else { return "..."; } } return str;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -