// Use Ajax to put example boxes or code examples into a page // Written by Jan Labanowski (jkl at ccl net) // Last modified on Dec 3, 2006 // // Anyone can use it, give me credit (or not), and send "thank yous", // but the script comes with no warranty, and if it hurts you // in any way, you agree not to sue me. Poniatno? // I will be very thankful for corrections and comments and I will // implement them and give you credit (I mean, as an acknowledgment // in this file, not money or something...). // // This is a short utility that I use to write documentation or // instructions. The idea is that in the HTML file the examples of // code or scripts have to be given for download as links and also shown // in the text. Since I am lazy, I prefer to have them only in one place. // It also makes the document maintenance easier when revisions are made. // I only need to modify text files that have the examples or code, // and do not have to edit the main document. // // How it works... In the main document, I put "place holders" for files // in the form of
tags with the id attribute starting from // "getfile". Inside the
...
I put information about // the file and the size of the element // (NO SPACES!!!). This will be replaced when document loads with // content of the files. // For example: //
rows=10;cols=60;url=examples/example1.pl
// will create and creates a link // (an = 0) { nLines++; // find the longest line if((newLineEnd - oldLineEnd + 1) > nColumns) { nColumns = newLineEnd - oldLineEnd + 1; } // prepare to search for next line end search oldLineEnd = newLineEnd + 1; } } // if rows attribute not given, set it here if(nR < 1) { nR = nLines+1; } if(nC < 1) { // if cols attribute not given, set it here nC = nColumns + 1; if(nR <= nLines) { // if scroll bar is present, add 2 for it nC += 2; } if((nC < 10) && (nR < 3)) { // for short files make it look good nC = gotText.length; } if(nC < 10) { // short textareas look bad nC = 10; } if(nC > 83) { // if long line, add scrollbars rather then width nC = 83; } } // write a textarea element with text to the
element // '+"
\n"; // alert(el.innerHTML); // uncomment this to check what it does } else { // status is not OK and file was not found. el.innerHTML = "Failed to retrieve "+url+" Status="+req.status+ " Server Resonse: "+req.statusText; } } } req.open("GET", url+stamp, true); // set up Ajax GET method to the server req.send(""); // send request to server -- we use GET method so } // nothing is send in the request body function getMyFiles() { // get files to load from server to
...
var elements = new Array(); // set up local array for
elements elements = document.getElementsByTagName("div"); // get all
var i, j, el, content, nR, nC, url, id; // local variables var attributes = new Array(); // array to hold parsed info var pair = new Array(); // array to hold key/value pairs for(i = 0; i < elements.length; i++) { // scan all retrieved elements el = elements[i]; id = el.getAttribute('id'); // get the value of id attribute of
if(! id) { // standard browsers have el.hasAttribute() continue; // method but IE6 is better and does not need it } if(id.indexOf('getfile') != 0) { // if id is not getfile.* skip it continue; // since it is something else } id = id + '_' + i + '_ta'; // create unique id for the