[Tutorial] Print links in printable version using javascript

This is a discussion on "[Tutorial] Print links in printable version using javascript" within the JavaScript Forum section. This forum, and the thread "[Tutorial] Print links in printable version using javascript are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jan 18th, 2008, 08:28
Reputable Member
Join Date: Nov 2007
Location: India
Posts: 150
Blog Entries: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Arrow [Tutorial] Print links in printable version using javascript

With screen media, one can easily link pages on the web(Eg:Click here to visit Google) . But when a user 'prints' the page, all your links will probably be lost or atleast won't print as you want them. To avoid this, it would be nice if we could somehow bundle the links with the printed matter. That is what I recently wanted for my website and I found that javascript could easily help me.

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!
  1. 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.
  2. Calculate size of the above obtained array using links.length function.
  3. 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.
So, our code would look like below
HTML: Select all
var links=document.getElementsByTagName("a"); // returns an array of all hyperlinks
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
}
To 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.

Source: My Web development blog (originally written by me)
Last Blog Entry: Cross browser nuisance (Feb 11th, 2008)

Last edited by c010depunkk; Jan 18th, 2008 at 09:38. Reason: please use [HTML] tags when posting HTML
Reply With Quote

  #2 (permalink)  
Old Jan 18th, 2008, 08:45
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: [Tutorial] Print links in printable version using javascript

Wouldn't it be better to actually include the hyperlink in it's original context?

Something like :

Code: Select all
var aLinks = document.getElementsByTagName('a');

var nm = aLinks.length;

for (i=0; i<nm; i++)
  { 
aLinks[i].originalText = aLinks.innerHTML; // Store original text
// Now amend the link
aLinks[i].innerHTML += ' (' + (aLinks[i].title? aLinks[i].title + ' : ' : '') + aLinks[i].href + ')';
// The above will leave original link text but apend the title (if included) and the href in brackets after the link text.
}

// now print the page
window.print();

// now reset the links

for (i=0; i<nm; i++)
{
    aLinks[i].innerHtml = aLinks[i].originalText;
}

That would save printing links down the bottom of the page.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #3 (permalink)  
Old Jan 18th, 2008, 10:18
Reputable Member
Join Date: Nov 2007
Location: India
Posts: 150
Blog Entries: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: [Tutorial] Print links in printable version using javascript

Quote:
Originally Posted by Rakuli View Post
Wouldn't it be better to actually include the hyperlink in it's original context?

Something like :

Code: Select all
var aLinks = document.getElementsByTagName('a');

var nm = aLinks.length;

for (i=0; i<nm; i++)
  { 
aLinks[i].originalText = aLinks.innerHTML; // Store original text
// Now amend the link
aLinks[i].innerHTML += ' (' + (aLinks[i].title? aLinks[i].title + ' : ' : '') + aLinks[i].href + ')';
// The above will leave original link text but apend the title (if included) and the href in brackets after the link text.
}

// now print the page
window.print();

// now reset the links

for (i=0; i<nm; i++)
{
    aLinks[i].innerHtml = aLinks[i].originalText;
}

That would save printing links down the bottom of the page.
You mean it would write the HREF attribute of the hyperlink instead of the text inbetween <a> and </a> tags?
Last Blog Entry: Cross browser nuisance (Feb 11th, 2008)
Reply With Quote
  #4 (permalink)  
Old Jan 18th, 2008, 10:30
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: [Tutorial] Print links in printable version using javascript

No it would write the text between the <a> tags and then in parenthesis would write the title of the link (if one exists) and the href.

say I had a link like this

<a href="http://www.example.com" title="Example Website">Hello Rohan!</a>

The printed page would read

Hello Rohan! (Example Website : http://www.example.com)
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #5 (permalink)  
Old Jan 18th, 2008, 11:14
Reputable Member
Join Date: Nov 2007
Location: India
Posts: 150
Blog Entries: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: [Tutorial] Print links in printable version using javascript

Quote:
Originally Posted by Rakuli View Post
No it would write the text between the <a> tags and then in parenthesis would write the title of the link (if one exists) and the href.

say I had a link like this

<a href="http://www.example.com" title="Example Website">Hello Rohan!</a>

The printed page would read

Hello Rohan! (Example Website : http://www.example.com)
yeah, this one is a nice and persuasive method
Last Blog Entry: Cross browser nuisance (Feb 11th, 2008)
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Javascript tutorial hegerp JavaScript Forum 2 Nov 29th, 2007 13:31
[Tutorial -> Trick] Link checking using JavaScript (ny myself) RohanShenoy JavaScript Forum 7 Nov 8th, 2007 12:52
Newbie, Please help with javascript links lea JavaScript Forum 1 Nov 9th, 2006 14:45
Detecting JRE version using javascript Nagendra JavaScript Forum 0 Sep 18th, 2006 05:08


All times are GMT. The time now is 05:57.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43