PDA

View Full Version : Simple additions


fside
07-01-2009, 05:25 AM
Some simple additions I've found useful.

1) A substitute for Stephenson's dollar sign. His does much more than just look up elements by id. This, here, only does that.

jQuery, of course, also uses the $ as its top object. But it also has other alternative names, like "jQuery" itself. To use this, you can use those, or name jQuery whatever else, or use the "noconflict" method in jQuery, etc. Just using "jQuery", or renaming that, in your script is probably the easiest.

So I show loading jQuery, first, just to show. And then I show the $ function. And it has an attached cache. And that intercepts the jQuery $ within the scope of your script. This means previously retrieved object references are stored, instead of being retrieved again. It's been shown this DOES save compute time.

In addition, the cache can become unreliable if objects are removed, say by innerHTML, and new ones created with the same name. So certain innerHTML it is assumed will go through a function, instead of using innerHTML directly. Part of that function will be to call the purge function shown below.

2) A bug window that is just a dump of whatever expressions, comma separated. Last go top of stack. So top of the 'watch' window are the most recently dumped by this "bug" function.

And this assumes an anonymous wrapper, exposing only one name for your top object, just like Yahoo, and jQuery and the rest do.



/* the self-executing anonymous function - pretty much demanded in code, these day */
(function(){

/* Your top object, domain, name - lack of "var" makes this a property of the Window object */
DN = {
version: 'xx',
revision: {
number: 1,
date: 'the date'
}
};
/* Some alternative name, in case of conflicts */
DN1 = DN;

/* In goes the jQuery */
var scr=document.createElement("SCRIPT");
scr.src= PgCs.relDrcty + "scripts/jscript/jquery.js";
document.getElementsByTagName("HEAD")[0].appendChild(scr);

/* Now the new $ fn ("els" is the cache) */
function $(strID){ return $.els[strID] = $.els[strID] || document.getElementById(strID); }
$.els = { }

/* And the purge, called whenever deleting elements */
DN.purgeEventFns = function DN215(el){
var a = el.attributes, i, len, n;
if (a) {
len = a.length;
for (i = 0; i < len; i += 1) {
n = a[i].name;
if (typeof el[n] === 'function'){ el[n] = null; }
}
}
/* And null out any in the cache */
if (el.id){ $.els[el.id] = null; }
a = el.childNodes;
if (a) {
len = a.length;
for (i = 0; i < len; i += 1) {
pub.purgeEventFns(el.childNodes[i]);
}
}
};

.
.
.

var C = {
debug: "debug",
container: "mainContainer"
}

/*
* And the other is a simple 'watch' window
* Call as "bug(expr, expr, . . . )"
*/
DN.bug = function bug(){
var oc = $(C.debug);
if (oc){
oc.innerHTML = "<br>" +oc.innerHTML;
oc.style.display = "block";
for (var i=arguments.length-1; i >-1; i-=1){ oc.innerHTML = [i>0 ? ", " : "", arguments[i]].join("") +oc.innerHTML; }
}
};
/* "top" because the context could be inside a frame or iframe */
var bug = top.DN.bug;

.
.
.

/*
* And somewhere else, the bug window is placed in the document
*
* "container" is a div you place just under, first child of, BODY - most people use such, by whatever name.
* The onclick simply clears the box - not necessary
* The "appendEl" is whatever one uses to add a new element to the document,
* there's an example I suggested, last year, on the Javascript Script forum, on this site
*/
DN.appendEl(C.debug, C.container, "div", "onclick='this.innerHTML=\"\"'");

.
.
.

/* This was composed for Coding Forum using "Its All Text!" plugin for Firefox. */

})();

fside
07-05-2009, 06:01 AM
I should note:

"PgCs.relDrcty"

The PgCs object is something I include on each generated web page. And relDrcty is the relative root directory given that particular web page. In other words, it consists of none, one or more "../" in a row. And that is prefixed to the known subpath and filename.

But you could do similar simply looking at the top.location URL and finding how far you are in by first isolating the known DNS name, etc, however you wish to do it. But that's what PgCs and particularly PgCs.relDrcty mean.