﻿// JScript File
<PRE lang=jscript id=pre3 style="MARGIN-TOP: 0px">// 
// IE only: attach eventhandlers.
// They trigger when a user  prints a web page; 
//     From File->Print, File->Print Preview or
//     by executing a window.print() command 
//
window.onbeforeprint=beforePrint
window.onafterprint=afterPrint

function beforePrint() {
    appendlinks(content);
}

function afterPrint() {
    removelinks(appendedlinks);
}


//
// Generic print function, called from the web page
//
function printContent() {
    if (window.print) {
           window.print();
    } else {
        alert("your browser doesn't support this function")
    }
}


// 
// When appending links to the document, do not 
// append links pointing to the following domains / sites
//

var excludedomains=["soderlind.no", "soderlind.org","localhost"]
var excludedomains=excludedomains.join("|")
rexcludedomains=new RegExp(excludedomains, "i")

//
// appendlinks(id)
// id = id of the part of the web page you want to extract links from
//      ex: document.body
//

function appendlinks(id){
    
    var strD = "<p/>";
    var num = 0;
    if (document.getElementById){
        var links=id.getElementsByTagName("A")    
        var total=links.length
        strD += "<dl id=\"appendedlinks\" border=0>"
        strD += "<dt><h2>Links extracted from the document:</h2></dt>"
        for (i=0;i<total;i++){
            if (links[i].hostname.search(rexcludedomains)==-1 
                && links[i].href.indexOf("http:")!=-1) {
                strD += '<dt>'+links[i].innerText+'</dt>'
                strD += '<dd>'+links[i]+'</dd>'
                num++;
            }
        }
        strD += "</dl>"

        if (id.insertAdjacentHTML && num>0)
            id.insertAdjacentHTML("beforeEnd",strD);
    }
}

//
// removelinks(id)
// After the print job is done, remove the appended links from the document
// id = appendedlinks
//
function removelinks(id) {
    if (document.getElementById){
        id.removeNode(true);
    }
}</PRE>
