Robert Lawrence

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromRobert Lawrence
ToMe
Subjectjavascript
Date17 October 2003 15:06
Hi,

 I enjoyed the content of your site but I find your code examples 
extremely hard to read, mainly due to the code formatting style. To read 
the code I had to re-format it, I would suggest adding white space and 
blank lines to give the reader more visual cues. Anyway, good algorithms.

Best of luck

I hope you read this here because your email server is having problems and rejected my email to you:

FromMe
ToRobert Lawrence
SubjectRe: javascript
Date17 October 2003 15:27
Sorry you are having problems with it. I do try to make the site as
accessible as possible, including trying to make the code examples easy to
read. I am limited to the fact that it has to work in all screen
resolutions, so long lines of code tend to wrap onto the next line. For
this reason, I do occasionally have to use a smaller font size (such as on
the section about window size and scrolling:
http://www.howtocreate.co.uk/tutorials/javascript/browserwindow). I do also
try to use different colours to help distinguish code from surrounding text
(the colours stand out less if you use a low colour stylesheet).

Still, you are having problems using it, so enough excuses, time for
solutions.

Could you send an example of how you would like it to look (so how it looks
after you re-formatted it), and if possible, a screenshot of how it
normally looks in your browser, the browser you are using, screen
resolution, operating system etc, and I will see what I can do for you.

Tarquin - author of http://www.howtocreate.co.uk/
FromRobert Lawrence
ToMe
SubjectRe: javascript
Date 22 October 2003 22:10
Hi,

Sorry I have not goten back sooner, I have been having a hectic week.
Well here is my reformatted and a little refactored version of your
function "reWriteDiv". The blank lines could be gotten rid of and some
statements could be placed on seperate lines.

Good Luck - Rob

[ED. WARNING: this version of the script does not actually work;
'else ((typeof' should be 'else if ((typeof'
also, each '.replace' line in 'replaceSpecials' should be preceded by:
'nString = '
also, a few comments are misplaced]

function reWriteDiv(oDiv, oFrame, oString, oBGCol, oWidth, oHeight, oBorder, oRepeat)
{
    //N.B. made the !oRepeat the else case
    if (oRepeat)
    {
        oString = unescape(oString);
    }
    else ((typeof(oWidth) != 'undefined') && (typeof(oHeight) != 'undefined')  && (typeof(oBorder) != 'undefined'))
    {
        //N.B. broke up into smaller parts
        oString =  '<table border="' + oBorder + '" cellpadding="0" cellspacing="0">';
        oString += '<tr>';
        oString += '<td height="' + oHeight + '" width="' + oWidth + '" valign="top"></td>';
        oString += '</tr>';
        oString += '</table>';
    }
    
    var oContent = getRefToDivNest(oDiv); //create fake objects if needed
    if (!oContent)
    {
        oContent = new Object();
    }
    
    if (!window.frames)
    {
        window.frames = new Object();
    }
    
    if (typeof(oContent.innerHTML) != 'undefined')
    {
        oContent.innerHTML = oString; //DOM
        if (oContent.style)
        {
            oContent.style.backgroundColor = oBGCol;
        }
        return;
    }
    
    //& Proprietary DOM
    if (oContent.document == document || !oContent.document)
    {
        if (!window.frames.length)
        {
            window.clearTimeout(winStatTim);
            rotString = '   -------  ' + replaceSpecials(oString); //N.B. added a helper function below
            startrot(rotString.length);
            return;
        }
        
        //use status bar. Clears HTML ( clears script & CSS if in comments )
        if (!oRepeat)
        {
            window.clearTimeout(iframeHasNotLd[oFrame]);
        }
        
        //if they rewrite more than once before the iframe loads, only show the last one
        if (!window.frames[oFrame])
        {
            //the iframe is unavailable until its content has loaded
            iframeHasNotLd[oFrame] = window.setTimeout('reWriteDiv(\''+oDiv+'\',\''+oFrame+'\',\''+escape(oString)+'\',\''+oBGCol+'\','+oWidth+','+oHeight+','+oBorder+',true)', 100);
            return;
        }
        
        oContent = window.frames[oFrame].window;
    }
    
    //use iframe
    
    //N.B. broke up into smaller parts
    var iframeHTML = '<html>';
    iframeHTML    += '<head><title>Dynamic content</title></head>';
    iframeHTML    += '<body bgcolor="' + oBGCol + '">';
    iframeHTML    += oString;
    iframeHTML    += '</body>';
    iframeHTML    += '</html>';
    
    oContent.document.open();
    oContent.document.write(iframeHTML);
    oContent.document.close();
}

function replaceSpecials(oString)
{
    var nString = oString;
    
    nString.replace(/<!--([^>]|[^-]>|[^-]->)*-->/g, ' ');
    
    //N.B. could this regular expression be broken up into smaller parts?
    //   [ED. No. It matches both valid and invalid (x)html tags]
    nString.replace(/<[\/!]?[a-z]+\d*(\s+[a-z][a-z\-]*(=[^\s>"']*|="[^"]*"|='[^']*')?)*\s*(\s\/)?>/gi, ' ');

    nString.replace(/[^\S ]+/g, ' ');
    nString.replace(/&lt;/g, '<');
    nString.replace(/&gt;/g, '>');
    nString.replace(/&nbsp;/g, ' ');
    nString.replace(/&quot;/g, '"');
    nString.replace(/&amp;/g, '\t');
    nString.replace(/&#?\w+;/g, ' ');
    nString.replace(/\t/g, '&');
    nString.replace(/ +/g, ' ');
    return nString;
}
FromMe
ToRobert Lawrence
SubjectRe: javascript
Date 23 October 2003 19:52
Ah. I had worried that you had meant that;
not so much the code examples, but the JavaScript libraries/header files.

The thing is that those were not originally intended to be examples, and
are meant to be real scripts for real use, not tutorials. I included them
on the 'examples' page as they can be viewed as examples of real
programming, not lessons on how to program. I agree that they are not
always easy to read, but that is mainly because they are optimised for real
use.

I apologise for any difficulty this may cause, but as I have currently got
about 40 of these scripts, I do not have the time, (or the manpower) to
continually produce extra readable versions of the library files.

However, I thank you for the work you have gone to in reformatting this
script, and will put a link to your version to show it as a more readable
version, to give a comparison between 'real use' and tutorial scripts.
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.