Create a JS Code Base page 1/2

Source: Internet
Author: User

The background program filters out single quotes. In some cases, if a space exists, it indicates single quotes.
/**
* @ Author Super Sha
* QQ: 770104121
* Email: supersha@foxmail.com
* All Rights Reserved!
*/
(Function () {// original
Function $ (s ){
If (! S)
Return false;
Return s. constructor = String? Document. getElementById (s): s;
}
Window. $ =$;
})();

(Function () {// anonymously encapsulate and execute functions. The functions or variables defined in this function can be generic and cannot be accessed outside the function unless you declare them in the namespace.
If (! Window. Susa)
Window [Susa] ={}; // declare the Susa namespace
/*
* Get element reference
*/
Function $ (){
Var elems = new Array ();
For (var I = 0; I <arguments. length; I ++ ){
Var elem = arguments [I];
If (typeof arguments [I] = "string") {// type check
Elem = document. getElementById (arguments [I]);
}
// Here, the double-off function indicates that if the input is a string and only one parameter, or if the input is a DOM reference, the DOM node reference is returned.
If (arguments. length = 1 ){
Return elem;
}
Else {
Elems. push (elem );
}
}
Return elems;
}
Window [Susa] [$] =$;

/*
* Return the child element in a specific element, or the document
*/
Function tag (type, elem ){
Elem = elem | document;
Type = type | "*"; // if no parameter exists, the reference of all elements in the document is returned.
Return elem. getElementsByTagName (type );
}
Window [Susa] [tag] = tag;

/*
* Return the value of the input box.
*/
Function value (id ){
Var elem = $ (id); // call the method defined in the anonymous Function
If (elem! = Null & (elem. nodeName = INPUT | elem. nodeName = TEXTAREA )){
Return elem. value;
}
}
Window [Susa] [value] = value;

/*
* StringBuilder objects that can be quickly connected to strings // (original)
*/
Var StringBuilder = {
_ Arr: new Array,
Append: function (str ){
If (typeof str = "string "){
This. _ arr. push (str );
}
Return this;
},
ToString: function (){
If (this. _ arr. length! = 0 ){
Var strs = this. _ arr. join (,);
This. _ arr = []; // solves the problem of repeated return addition.
Return strs;
}
Else {
Return null;
}
}
}
// Function appendString (){
// For (var I = 0; I <3; I ++ ){
// StringBuilder. append ("test ");
//}
// Return StringBuilder. toString ();
//}
// Window [Susa] [str] = appendString;

/*
* AddEvent and romoveEvent Methods
*/
Function addEvent (elem, type, fn ){
If (elem! = $ (Elem) | elem = null)
Return false;
If (type = null | fn = null)
Return false;

If (elem. addEventListener) {// W3C Method
Elem. addEventListener (type, fn, false );
Return true;
}
Else
If (elem. attachEvent) {// IE Method
// Node [e + type + fn] = fn;
/// Node [type + fn] = function (){
// Node [e + type + fn] (window. event); // I don't know why.
//}
// Node. attachEvent (on + type, node [type + fn]);
// Return this;
Elem. attachEvent (on + type, function (){
Fn. call (elem)
});
// Note: Here, an anonymous function is used to prevent this object pointing to errors in IE.
Return this;
}
Else {
Elem ["on" + type] = function (){
Fn. call (elem)
};
}
}
Function removeEvent (elem, type, fn ){
If (elem! = $ (Elem) | elem = null)
Return false;
If (type = null | fn = null)
Return false;

If (elem. removeEventListener) {// W3C Method
Elem. removeEventListener (type, fn, false );
Return true;
}
Else
If (elem. detachEvent) // method of IE
{
// Node. detachEvent (on + type, node [type + fn]);
Elem. detachEvent (on + type, fn );
Return this;
}
}
Window [Susa] [addEvent] = addEvent;
Window [Susa] [removeEvent] = removeEvent;

/*
* The getElementsByClassName method returns references to all elements that match a specific class. The tag and elem parameters are optional.
*/
Function getElementsByClassName (classname, tag, elem ){
Elem = elem | document;
If (elem! = $ (Elem) | elem = null)
Return false;
// Pay attention to the usage of parent. all in this function. It is used to confirm whether the parent is document and distinguishes IE and Ilia.
If (! Tag)
Tag = "*";
Var allTags = (tag = * & elem. all )? Elem. all: elem. getElementsByTagName (tag );

// Create a regular expression to check whether it contains the specified class name
Classname = classname. replace (/\-/g ,"\\-");
Var regex = new RegExp ("(^ | \ s *)" + classname + "(\ s * | $ )");

Var matchElements = new Array ();
Var elem;
For (var I = 0; I <allTags. length; I ++ ){
Elem = allTags [I];
If (regex. test (elem. className) {// check the class name based on the regular expression.
MatchElements. push (elem );
}
}

Return matchElements;
}
Window [Susa] [getElementsByClassName] = getElementsByClassName;

/*
* The toggleDisplay method switches the visibility of HTML tags.
*/
Function toggleDisplay (id, value ){
Var elem = $ (id );
If (elem! = $ (Elem) | elem = null | elem. nodeType! = 1)
Return false;
If (elem. style. display! = "None "){
Elem. style. display = "none ";
}
Else {
Elem. style. display = value |;
}
Return true;
}
Window [Susa] ["toggleDisplay"] = toggleDisplay;

/*
* InsertAfter Method
*/
Function insertAfter (node, referenceNode ){
If (node! = $ (Node) | node = null)
Return false;
If (referenceNode! = $ (ReferenceNode) | referenceNode = null)
Return false;

Return referenceNode. parentNode. insertBefore (node, referenceNode. nextSibling); // use the insertBefore method if the second parameter
} // If it is null, It is inserted to the end of parentNode.
Window [Susa] [insertAfter] = insertAfter;

/*
* RemoveChildren method: deletes all child elements under the current node.
*/
Function removeChildren (parent ){
If (parent! = $ (Node) | parent = null)
Return false;
While (parent. firstChild) {// Delete nodes cyclically
Parent. firstChild. parentNode. removeChild (node. firstChild );
}
Return parent;
}
Window [Susa] [removeChildren] = removeChildren;

/*
* The prependChild method inserts the selected node to the beginning of the current node.
*/
Function prependChild (parent, newNode ){
If (parent! = $ (Parent) | parent = null)
Return false;
If (newNode! = $ (NewNode) | newNode = null)
Return false;

If (parent. firstChild) {// A subfunction exists during judgment.
Parent. insertBefore (newNode, parent. firstChild );
}
Else {
Parent. appendChild (newNode );
}
Return parent;
}
Window [Susa] [prependChild] = prependChild;

/*
* BindFunction () method, used to adjust the execution environment of this
*/
Function adjustFunc (obj, func) {// adjust the execution environment of func to obj.
Return function () {// return the reference of an anonymous function
Func. apply (obj, arguments );
};
}
Window [Susa] [adjustFunc] = adjustFunc;

/*
* Get the width and height of the display window, and return an object containing the width and height attributes. It is not public and is only called by other methods in this anonymous function.
*/
Function getBrowserWindowSize (){
Var de = document.doc umentElement; // get the root node
Var obj = {
Width: (window. innerWidth | (de. clientWidth) | document. body. clientWidth ),
Height: (window. innerHeight | (de. clientHeight) | document. body. clientHeight)
}

Return obj;
}

/*
* Debug log object
*/
Function log (id ){
Id = id | SusaLogWindow;
Var logWindow = null; // Private Attribute, used in each
Var createWindow = function () {// Private method, used to dynamically generate a list Node
If (! Document. body ){
Alert (document. body hasn \ t finished loading .);
Return;
}
Var browerWindowSize = getBrowserWindowSize ();
Var top = (browerWindowSize. height-200)/2) | 0; // obtain the position of the upper left corner of the window in the browser.
Var left = (browerWindowSize. width-200)/2) | 0;

LogWindow = document. createElement (ul); // dynamically generate a UL Element
LogWindow. setAttribute (id, id );

LogWindow. style. position = absolute; // modify the UL element.
LogWindow. style. top = top + px;
LogWindow. style. left = left + px;

LogWindow. style. width = 250px;
LogWindow. style. height = 200px;
LogWindow. style. overflow = scroll;

LogWindow. style. padding = 0;
LogWindow. style. margin = 0;
LogWindow. style. border = 1px solid black;
LogWindow. style. backgroundColor = white;
LogWindow. style. listStyle = none;
LogWindow. style. font = 10px/10px Verdana, Tahoma, Sans;

Document. body. appendChild (logWindow); // Add dynamically generated nodes to the body
}

This. writeRaw = function (message) {// privileged method, used to add LI content to the UL node generated by the createWindow method. The instance must be declared before calling
If (! LogWindow)
CreateWindow (); // if the initial window does not exist, create it
Var li = document. createElement ("li"); // dynamically generate a LI Element
Li. style. padding = 2px;
Li. style. border = 0;
Li. style. borderBottom = 1px dotted black;
Li. style. margin = 0 2px;
Li. style. color = #000;
Li. style. font = 9px/9px Verdana, Tahoma, Sans;

If (typeof message = undefined ){
Li. appendChild (document. createTextNode (Message was undefined! ));
}
Else
If (typeof li. innerHTML! = Undefined ){
Li. innerHTML = message;
}
Else {
Li. appendChild (document. createTextNode (message ));
}

LogWindow. appendChild (li); // Add the generated LI node to logWindow.
Return true;
}
}

Log. prototype = {
Write: function (message ){
If (arguments. length = 0) {// The warning message parameter is null.
Return this. writeRaw ("Lack of params! ");
}

If (typeof message! = "String") {// If the input parameter is not a string, call the toString method. If the access does not exist, record the object type.
If (message. toString)
Return this. writeRaw (message. toString (); // note the following method: message. toString.
Else
Return this. writeRaw (typeof message );
}
Message = message. replace (/</g, "<"). replace (/>/g, ">"); // filter <> left and right angle brackets
Return this. writeRaw (message );
},
Header: function (message) {// write a title to the log window
Message = --> <span style = "color: red; background: # eee; font-weight: bold;"> + message + </span> <--;
Return this. writeRaw (message );
}
}
Window [Susa] [log] = new log (); // note that the declared object should be displayed, because the constructor uses the reference of the privileged method this.
/*
* A self-created debugging function (original) uses the name of the function to be tested as the debug parameter and supports multiple parameters // (original)
*/
Function debug (){
For (var I = 0; I <arguments. length; I ++ ){
If (typeof arguments [I]! = Function ){
Alert ("Params socould be Function type! ");
Return;
}
}
Var args = arguments;

(Function () {// encapsulate the process of executing the function
Try {
For (var I = 0; I <args. length; I ++ ){
Args [I] (); // executes the Function
}
}
Catch (ex ){
Susa. log. writeRaw (Error: + ex. message + "Line:" + ex. lineNumber );
}
})();
}
Window [Susa] [debug] = debug;

/*
* Declare and judge the constant of the node type // original
*/
Window [Susa] [node] = {
ELEMENT_NODE: 1,
ATTRIBUTE_NODE: 2,
TEXT_NODE: 3,
CDATA_SECTION_NODE: 4,
ENTITY_REFERENCE_NODE: 5,
ENTITY_NODE: 6,
PROCESSION_INSTRUCTION_NODE: 7,
COMMENT_NODE: 8,
DOCUMENT_NODE: 9,
DOCUMENT_TYPE_NODE: 10,
DOCUMENT_FRAGMENT_NODE: 11,
NOTATION_NODE: 12
};

/*
* Function for detecting the degree to which the browser supports DOM Level (original)
*/
Function CheckDOMLevel (){
If (document. implementation) {// determines whether the document. implementation attribute is supported
Var DOM = ["Core", "XML", "HTML", "Views", "SytleSheets", "CSS", "CSS2", "Events", "UIEvents ", "MouseEvents", "MutationEvent", "HTMLEvents", "Range", "Traversal", "LS", "LS-Async", "Validation"];
Var Level = ["1.0", "2.0", "3.0"];

For (var I = 0; I <DOM. length; I ++ ){
For (var j = 0; j <Level. length; j ++ ){
If (document. implementation. hasFeature (DOM [I], Level [j]) {// document. implementation. hasFeature accepts two parameters.
Susa. log. writeRaw ("<span style = color: #0c0;> DOM" + Level [j] + "" + DOM [I] + "Supported. </span> ");
}
Else {
Susa. log. writeRaw ("<span style = color: # c00;> DOM" + Level [j] + "" + DOM [I] + "Not Supported! </Span> ")
}
}
}
}
Else {
Susa. log. write ("<span style = color: # c00;> <B> No DOMImplementation Supported! </B> </span> ");
}
}
Window [Susa] [CheckDOMLevel] = CheckDOMLevel;

/*
* Get and set the element feature value. You can get the getter and set the setter call method.
*/
Function attr (elem, name, value ){
If (! Name | name. constructor! = String)
Return;

// Check whether the name is out of a weird name
Name = {
For: htmlFor,
Class: className
} [Name] |
Name;

If (typeof value! = Undefined ){
Elem [name] = value; // use the shortcut first
If (elem. setAttribute) {// Yes, use setAttribute
Elem. setAttribute (name, value );
}
}
Return elem [name] | elem. getAttribute (name) |; // return the feature value
}
Window [Susa] [attr] = attr;

/*
* Create a common function for the new DOM Element
*/
Function create (label ){
Return document. createAttributeNS? Document. createElementNS (http://www.w3.org/1999/xhtml, label): document. createElement (label); // return a reference to the new element
}
Window [Susa] [create] = create;

/*
* Create a TextNode Node Function
*/
// Function createText (elem) {// directly called by the DOM Element
// $ (Elem) = elem? This. appendChild (elem): this. appendChild (document. createTextNode (elem ));
// Return this;
//}
Function createText (parent, elem) {// (reference + original)
Return $ (elem) = elem? Parent. appendChild (elem): parent. appendChild (document. createTextNode (elem ));
}
Window ["Susa"] [createText] = createText;

/*
* Insert an element before another DOM element // (reference + original)
*/
Function before (newNode, referenceNode ){

If (! NewNode & $ (referenceNode )! = ReferenceNode)
Return false;
Var elems = checkElem (newNode); // obtain the array to be referenced by the node
For (var I = elems. length-1; I> = 0; I --){
ReferenceNode. parentNode. insertBefore (elems [I], referenceNode );
}
}
Window [Susa] [before] = before;

/*
* Append a child element to another element. elem can be a string with HTML tags or can be referenced by DOM element nodes. // (reference + original)
*/
Function append (parent, elem ){
If (! Elem & $ (parent )! = Parent)
Return false;
Var elems = checkElem (elem); // obtain the array to be referenced by the node
For (var I = elems. length-1; I> = 0; I --){
Parent. appendChild (elems [I]);
}
}
Window [Susa] [append] = append;

Function checkElem (elem) {// auxiliary functions of before and append functions // (reference + original)
Var r = [];
If (elem & elem. constructor = String) {// if the parameter is a String
Var div = create ("div ");
Div. innerHTML = elem;
For (var I = div. childNodes. length-1; I> = 0; I --){
R [r. length] = div. childNodes [I]; // Method for dynamically generating Arrays
}
}
Else
If (elem & elem. constructor = Array) {// if the parameter is an Array
Var div = create ("div ");
Div. innerHTML = elem. join ();
For (var I = div. childNodes. length-1; I> = 0; I --){
R [r. length] = div. childNodes [I]; // Method for dynamically generating Arrays
}
}
Else
If (elem & $ (elem) = elem) {// if it is a DOM Node
R [r. length] = elem;
}
Return r;
}

/*
* Delete all content in the DOM element. parameters can be referenced by DOM nodes or html id tags.
*/
Function empty (elem ){
Elem = transformLabelID (elem );
While (elem. firstChild ){
Elem. removeChild (elem. firstChild );
}
}
Window ["Susa"] [empty] = empty;

/*
* Html functions: getting and setting can be performed on DOM elements. elem can be a string or a reference to DOM elements. // (original)
*/
Function html (parent, elem ){
Parent = transformLabelID (parent );
If (elem & $ (elem) = elem ){
Parent. innerHTML = elem. innerHTML;
Return parent;
}
Else {
Return elem & elem. constructor = String? Parent. innerHTML = elem: parent. innerHTML;
}
}
Window ["Susa"] ["html"] = html;

/*
* The getting and setting functions can be used to obtain the content of element text.
*/
Function text (e ){
E = transformLabelID (e );
If (arguments [1]) {
E. innerHTML = arguments [1];
}
Else {
Var t = "";
E = e. childNodes | e; // If an element is input, the child element is traversed. Otherwise, it is assumed to be an array.
For (var j = 0; j <e. length; j ++ ){
T + = e [j]. nodeType! = 1? E [j]. nodeValue: text (e [j]); // call the text function recursively.
}
Return t;
}
}
Window [Susa] [text] = text;

/*
* OuterHTML and outerText methods similar to innerHTML and innerText // original
*/
Function outerText (elem, t ){
Elem = transformLabelID (elem );
// Check whether it is a DOM node or a text string
If (t ){
Var el = t. constructor = String? Document. createTextNode (t): t;
Elem. parentNode. insertBefore (el, elem); // insert a node before the current element
Elem. parentNode. removeChild (elem); // Delete the current node.
}
Else {
Return text (elem); // if the second parameter is null, the current Text
}

}
Function outerHTML (elem, h ){
Elem = transformLabelID (elem );
If (h ){
Var elems = checkElem (h); // returns the h string or the array of the node.
For (var I = elems. length-1; I> = 0; I --){
Elem. parentNode. insertBefore (elems [I], elem );
}
Elem. parentNode. removeChild (elem );
}
Else {
Var div = create ("div ");
Div. appendChild (elem );
Return div. innerHTML;
}
}
Window ["Susa"] ["outerText"] = outerText;
Window [Susa] ["outerHTML"] = outerHTML;

/*
* Wrag outsource the current element with another element // original
*/
Function wrag (elem, wragLabel, attrProp ){
Elem = transformLabelID (elem );
Var next = elem. nextSibling | document. body; // gets the next adjacent element of the elem element. If it does not exist, set it to the body.
Var w = create (wragLabel); // generates a new element that contains the current element.
For (var o in attrProp) {// set the attributes of the new element
W. setAttribute (o, attrProp [o]);
}
W. appendChild (elem );
Next = document. body? Document. body. appendChild (w): next. parentNode. insertBefore (w, next); // insert containing elements to the document
}
Window ["Susa"] ["wrag"] = wrag;

/*
* Convert the parameter to html id label as DOM node reference // original
*/
Function transformLabelID (str ){
Return! Str? False: $ (str );
}

/*
* Delete A single DOM node. The parameter can be a node reference or an html id tag value // (original)
*/
Function remove (elem ){
TransformLabelID (elem). parentNode. removeChild (transformLabelID (elem ));
}
Window [Susa] [remove] = remove;

/*
* The clone function returns a copy of the DOM element. The parameter can be a node reference or an html id tag value. // (original)
*/
Function clone (elem ){
Return transformLabelID (elem). cloneNode (true );
}
Window [Susa] [clone] = clone;

/*
* General functions that prevent Time bubbles
*/
Function stopBubble (e ){
If (e & e. stopPropagation) {// if a parameter is input, it is the w3c method.
E. stopPropagation ();
}
Else {
Window. event. cancelBubble = true; // method of IE
}
}
Window [Susa] [stopBubble] = stopBubble;

/*
* Common functions to prevent default browser behavior
*/
Function stopDefault (e ){
If (e & e. preventDefault) {// W3C Method
E. preventDefault ();
}
Else {
Window. event. returnValue = false; // IE Method
}
Return false;
}
Window ["Susa"] ["stopDefault"] = stopDefault;

/*
* Function for obtaining the true and final css style attribute values of Elements
*/
Function getStyle (elem, name ){
Elem = transformLabelID (elem); // The elem parameter can be a DOM reference or
If (elem. style [name]) {// if the attribute exists in style [], it has been set (and is the current one)
Return elem. style [name];
}
Else
If (elem. currentStyle) {// otherwise, try to use the IE Method
Return elem. currentStyle [name];
}
Else
If (document. defaultView & document. defaultView. getComputedStyle) {// or W3C method, if yes
// It uses general 'text-align 'style rules instead of textAlign
Name = name. replace (/([A-Z])/g, "-$1 ");
Name = name. toLowerCase ();
Return document. defaultView. getComputedStyle (elem, null). getPropertyValue (name );

}
Else {
Return null;
}
}
Window ["Susa"] [getStyle] = getStyle;

/*
* Function for converting word-word into wordWord format
*/
Function camelize (s ){
Return s. replace (/-(\ w)/g, function (strMatch, p ){
Return p. toUpperCase ();
});
}

/*
* Sets the css style of an element. The parameter is an object (original)
*/
Function setStyle (elem, obj ){
Elem = transformLabelID (elem );
For (var o in obj) {// traverses the properties of the obj Parameter
Elem. style [o] = obj [o]; // you can specify the css style.
}
}
Window [Susa] [setStyle] = setStyle;

/*
* Css functions, such as getter and setter, return the css style of a specific element // (original)
*/
Function css (elem, obj ){
Elem = transformLabelID (elem );
If (elem & (typeof obj = "string ")){
Return getStyle (elem, obj); // call the getStyle function to obtain the value of a specific css style.
}
Else
If (typeof obj = "object "){
For (var o in obj ){
Elem. style [o] = obj [o];
}
}
}
Window [Susa] [css] = css;

/*
* Merge two objects and integrate the Merged Results into the first object // original
*/
Function mergeObj (obj1, obj2 ){
If (typeof obj1 = "Object") & (typeof obj2 = "Object "))
Return false;
For (var o in obj2 ){
Obj1 [o] = obj2 [o];
}
Return obj1;
}
Window ["Susa"] ["mergeObj"] = mergeObj;

/*
* Input several function references, fetch the nearest non-error function and execute it (original)
*/
Function $ try (){
For (var I = 0; I <arguments. length; I ++ ){
Try {
Return arguments [I] (); // executes the Parameter Function
}
Catch (ex ){
Continue;
}
}
}
Window ["Susa"] ["$ try"] = $ try;

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.