Paste a JavaScript code that is commonly used in Mozilla

Source: Internet
Author: User
Tags contains return tagname window
Javascript

The unique definegetter and Definesetter in Mozilla make it possible to use the same method in Mozilla, and post some common code
For example
Obj.insertadjacenthtml, Currentstyle, Obj.attachevent, obj.detachevent and so on.

Copyright belongs to Erik Arvidsson, WEBFX

if (Browser.ismozilla) {//Set up IE environment for Moz

Extendeventobject ();
Emulateattachevent ();
Emulateeventhandlers (["Click", "DblClick", "MouseOver", "mouseout",
"MouseDown", "MouseUp", "MouseMove",
"KeyDown", "KeyPress", "KeyUp"]);

Emulatecurrentstyle ();
/*emulatedocumentall ();
Emulateelement ()
*/

It is better to use a constant for Event.button
Event.left = 0;
Event.middle = 1;
Event.right = 2;
}
else {
Event = {};
IE is returning wrong button number
Event.left = 1;
Event.middle = 4;
Event.right = 2;
}

/*
* Extends the Event object with Srcelement, cancelbubble, ReturnValue,
* Fromelement and Toelement
*/
function Extendeventobject () {
event.prototype.__definesetter__ ("ReturnValue", function (b) {
if (!b) This.preventdefault ();
return b;
});

event.prototype.__definesetter__ ("cancelbubble", function (b) {
if (b) this.stoppropagation ();
return b;
});

event.prototype.__definegetter__ ("Srcelement", function () {
var node = This.target;
while (Node.nodetype!= 1) node = Node.parentnode;
return node;
});

  event.prototype.__definegetter__ ("Fromelement", function () {
    var node;
    if (This.type = = "MouseOver")
      node = this.relatedtarget;
    else if (This.type = = "Mouseout")
      node = this.target;
    if (!node) return;
    while (node.nodetype!= 1) node = Node.parentnode;
    return node;
 });

event.prototype.__definegetter__ ("Toelement", function () {
var node;
if (This.type = = "Mouseout")
node = This.relatedtarget;
else if (This.type = "MouseOver")
node = This.target;
if (!node) return;
while (Node.nodetype!= 1) node = Node.parentnode;
return node;
});

event.prototype.__definegetter__ ("OffsetX", function () {
return This.layerx;
});
event.prototype.__definegetter__ ("OffsetY", function () {
return this.layery;
});
}

/*
 * emulates element.attachevent as as a detachevent
 */
function Emulateattachevent () {
  HTMLDocument.prototype.attachEvent =
  HTMLElement.prototype.attachEvent = function (Stype, fhandler) {
    var shorttypename = stype.replace (/on/, "");
    Fhandler._ieemueventhandler = function (e) {
      window.event = e;
      return Fhandler ();
   };
    This.addeventlistener (Shorttypename, Fhandler._ieemueventhandler, false);
 };

  HTMLDocument.prototype.detachEvent =
  HTMLElement.prototype.detachEvent = function (Stype, fhandler) {
    var shorttypename = stype.replace (/on/, "");
    if (typeof Fhandler._ieemueventhandler = = "function")
      This.removeeventlistener (Shorttypename, Fhandler._ieemueventhandler, false);
    Else
      this.removeeventlistener (Shorttypename, Fhandler, true);
 };
}

/*
* This function binds the event object passed along
* Event to Window.event
*/
function Emulateeventhandlers (eventnames) {
for (var i = 0; i < eventnames.length; i++) {
Document.addeventlistener (Eventnames[i], function (e) {
Window.event = e;
}, True); Using capture
}
}

/*
* Simple emulation of document.all
* This one is far from complete. Be cautious
*/

function Emulateallmodel () {
var allgetter = function () {
var a = This.getelementsbytagname ("*");
var node = this;
A.tags = function (stagname) {
Return Node.getelementsbytagname (Stagname);
};
return A;
};
HTMLDOCUMENT.PROTOTYPE.__DEFINEGETTER__ ("All", allgetter);
HTMLELEMENT.PROTOTYPE.__DEFINEGETTER__ ("All", allgetter);
}

function Extendelementmodel () {
htmlelement.prototype.__definegetter__ ("Parentelement", function () {
if (This.parentnode = = this.ownerdocument) return null;
return this.parentnode;
});

htmlelement.prototype.__definegetter__ ("Children", function () {
var tmp = [];
var j = 0;
var n;
for (var i = 0; i < this.childNodes.length; i++) {
n = this.childnodes[i];
if (N.nodetype = = 1) {
Tmp[j++] = n;
if (n.name) {//named children
if (!tmp[n.name])
Tmp[n.name] = [];
Tmp[n.name][tmp[n.name].length] = n;
}
if (n.id)//child with ID
Tmp[n.id] = n
}
}
return TMP;
});

HTMLElement.prototype.contains = function (OEL) {
if (Oel = =) return true;
if (OEL = null) return false;
Return This.contains (Oel.parentnode);
};
}

function Emulatecurrentstyle () {
htmlelement.prototype.__definegetter__ ("Currentstyle", function () {
Return This.ownerDocument.defaultView.getComputedStyle (this, null);
/*
var cs = {};
var el = this;
for (var i = 0; i < properties.length; i++) {
CS.__DEFINEGETTER__ (Properties[i], Encapsulateobjects (el, properties[i));
}
Return CS;
*/
});
}

function Emulatehtmlmodel () {

This function is used to generate a HTML string for the text properties/methods
It replaces ' \ n ' with ' <br ' > as OK as fixes consecutive white spaces
It also repalaces some special characters
function converttexttohtml (s) {
s = S.replace (/\&/g, "&"). Replace (/</g, "<"). Replace (/>/g, ">"). Replace (/\n/g, "<BR>");
while (/\s\s/.test (s))
s = S.replace (/\s\s/, "");
Return S.replace (/\s/g, "");
}

HTMLElement.prototype.insertAdjacentHTML = function (Swhere, SHTML) {
var df; : DocumentFragment
var r = This.ownerDocument.createRange ();

Switch (String (swhere). toLowerCase ()) {
Case "Beforebegin":
R.setstartbefore (this);
DF = r.createcontextualfragment (SHTML);
This.parentNode.insertBefore (DF, this);
Break

Case "Afterbegin":
R.selectnodecontents (this);
R.collapse (TRUE);
DF = r.createcontextualfragment (SHTML);
This.insertbefore (DF, this.firstchild);
Break

Case "BeforeEnd":
R.selectnodecontents (this);
R.collapse (FALSE);
DF = r.createcontextualfragment (SHTML);
This.appendchild (DF);
Break

Case "Afterend":
R.setstartafter (this);
DF = r.createcontextualfragment (SHTML);
This.parentNode.insertBefore (DF, this.nextsibling);
Break
}
};

htmlelement.prototype.__definesetter__ ("outerHTML", function (SHTML) {
var r = This.ownerDocument.createRange ();
R.setstartbefore (this);
var df = r.createcontextualfragment (SHTML);
This.parentNode.replaceChild (DF, this);

return SHTML;
});

  htmlelement.prototype.__definegetter__ ("Canhavechildren", function () {
     switch (this.tagname) {
      case ' area ':
       Case ' BASE ':
      case ' basefont ':
      case "COL":
      Case "FRAME":
      Case "HR":
       case ' IMG ':
      case ' br ':
      case INPUT ':
      case ' isindex ':
      case "LINK":
       case "META":
      Case "PARAM":
         return false;
   }
    return true;
 });

htmlelement.prototype.__definegetter__ ("outerHTML", function () {
var attr, attrs = this.attributes;
var str = "<" + this.tagname;
for (var i = 0; i < attrs.length; i++) {
attr = Attrs[i];
if (attr.specified)
STR + + "+ attr.name + ' =" ' + Attr.value + ' ";
}
if (!this.canhavechildren)
return str + ">";

return str + ">" + this.innerhtml + "</" + This.tagname + ">";
});

htmlelement.prototype.__definesetter__ ("InnerText", function (stext) {
this.innerhtml = converttexttohtml (stext);
return stext;
});

var tmpget;
htmlelement.prototype.__definegetter__ ("innertext", Tmpget = function () {
var r = This.ownerDocument.createRange ();
R.selectnodecontents (this);
return r.tostring ();
});

htmlelement.prototype.__definesetter__ ("Outertext", function (stext) {
this.outerhtml = converttexttohtml (stext);
return stext;
});
htmlelement.prototype.__definegetter__ ("Outertext", tmpget);

HTMLElement.prototype.insertAdjacentText = function (Swhere, stext) {
This.insertadjacenthtml (Swhere, converttexttohtml (stext));
};
}



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.