To see what I mean, see the below screenshot. It is a printable version of one the pages of my website.
The logic is to extract hyperlinks from the body of print matter and list them seperately near the footer. To see a live example, click the "Print this page" link in bottom-right of this page.
You would be shown as printer friendly format of that page with outbound links from the body-matter extracted and put in a seperate area.
So how do we do that?Its simple 3 steps!
- Use javascript function to getElementsByTagName. Since we need to get elements with "a" (anchor) tag, we collect all hyperlinks on by document.getElementsByTagName('a'). This will return an array of all hyperlinks in the matter. Lets call this array as links.
- Calculate size of the above obtained array using links.length function.
- Now lets run a loop that will execute it as many times as the no. of hyperlinks. In every loop, we perform three actions: 1. Get HREF attribute of a hyperlink. 2. Get TITLE attribute of the hyperlink and 3. We print the hyperlink's title and HREF attribute.
var links=document.getElementsByTagName("a"); // returns an array of all hyperlinksTo get this method working, you need to have the TITLE attribut for every link in your matter. If you don't have, then you may decide to skip the title and only print the HREF attribute.
var no=links.length;// Calculates no of hyperlinks
if(no>0) // execute further action only if atleast 1 hyperlink is found in the matter
{
document.write("Important links from this page have been given below. Please visit them:
")
for(i=0;iWe start the loop
{
href=document.links[i].href;//We obtained the HREF attribute of a hyperlink
var title=document.links[i].title;//We obtained the TITLE attribute of the same hyperlink
document.write(""+title+":
=>"+href+"
");} //Now we write the link
}
else
{
false;// return false if o hyperlink is found in the matter
}
No comments:
Post a Comment