[gutvol-p] Bookmark (javascript)

bert bertius at sympatico.ca
Sun Jan 9 19:51:43 PST 2005


This is the javascript version. Doubleclick on a paragraph to bookmark. 
After that reload the page and click the bookmark link to return to that 
paragraph. The bookmark is stored in a cookie so the script only works 
if cookie are not disabled. The script only works with HTML books that 
contains paragraph (as far as i know all of the HTML books do). 
Currently only works with Mozilla-powered browsers.

Add the following script just before the end of the body section (</body>):

<script>
init();

function init()
{
    var bookmark = getCookie('bookmark');
    if (bookmark == null)
    {
        bookmark = 0;
    }
    
    var p = document.getElementsByTagName('p');
    var b = document.getElementsByTagName('body');
    
    var a = document.createElement('a');
    a.href = '#bookmark';
    a.innerHTML = 'bookmark';
    b[0].insertBefore(a, p[0]);
    
    for (var i = 0; i < p.length; i++)
    {
        if (i == bookmark)
        {
            a = document.createElement('a');
            a.name = 'bookmark';
            a.innerHTML = '<hr />';
            b[0].insertBefore(a, p[i]);
            p[i].style.backgroundColor = 'lavender';
        }
        p[i].ondblclick = highlight;
        p[i].id = i;
    }
}

function highlight(e)
{
    var p = document.getElementsByTagName('p');
    for (var i = 0; i < p.length; i++)
    {
        p[i].style.backgroundColor = 'white';
    }
    
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;

    targ.style.backgroundColor = 'lavender';
    
    setCookie('bookmark', targ.id, 365);
}
        
function getCookie(NameOfCookie)
{
    if (document.cookie.length > 0)
    {
        var begin = document.cookie.indexOf(NameOfCookie+"=");
        if (begin != -1) // Note: != means "is not equal to"
        {
            begin += NameOfCookie.length+1;
            var end = document.cookie.indexOf(";", begin);
            if (end == -1) end = document.cookie.length;
                return unescape(document.cookie.substring(begin, end));
        }
    }
    return null;
}

function setCookie(NameOfCookie, value, expiredays)
{
    var ExpireDate = new Date ();
    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 
1000));

    document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays 
== null) ? "" : "; expires=" + ExpireDate.toGMTString());
}
</script>



More information about the gutvol-p mailing list