views:

1145

answers:

3

How to create a bookmark button that sends the whole page as email .

A: 

Put something like this within the head section of your HTML document, or better yet, skip the script tags and

put it in an external file and link to it in the head section instead:

<script language="JavaScript" type="Text/Javascript"><!-- // 
var url = " "; //u can add ur url
var pageName = "Client side programming"; 
function bookmark() { 
if (window.external) { 
window.external.AddFavorite(url, pageName)   
} 
else { 
alert("Sorry! Your browser doesn't support function. 
Use the bookmark option in your browser instead."); 
} 
} // --></script>

And this somewhere in the body section of your HTML document:

<input type="button" name="Bookmark this page" onclick="bookmark()" />

or...

Simple link:
<a
href="javascript:bookmark()">Click here to bookmark this page
</a>
Jugal
This will only bookmark the page, but won't send it in an email.
Kirtan
+3  A: 

HTML content of a page is a bit harder to send, you can use this to send the page's text in an email like this -

if (document.all)
    window.open("mailto:[email protected]?body=" + document.body.innerText); //For IE
else
    window.open("mailto:[email protected]?body=" + document.body.textContent); //For FF et al.

The bookmark will be -

"javascript:if (document.all) window.open('mailto:[email protected]?body=' + document.body.innerText); else window.open('mailto:[email protected]?body=' + document.body.textContent);"

EDIT: For this to work, you'll have to create a link, and you'll have to right-click it and click "Add to Favorites".

<a href="javascript:if (document.all) window.open('mailto:[email protected]?body=' + document.body.innerText); else window.open('mailto:[email protected]?body=' + document.body.textContent);">Bookmark This</a>
Kirtan
But i am in need of whole content
yesraaj
+1 for above code
yesraaj
@Kirtan - This didn't even occur to me. Nice code. +1
Jose Basilio
but the code does not work
yesraaj
A: 

I played with your question a little bit, done it with jquery ($('html').html()), and everything seemed fine for a moment.. BUT.. after I tried to email the whole content it didn't work either. When I got why it did not work I laughed - url too long, dude, software won't open it. Tested even on hard-coded string - short content opened the mail client fine, long did not.

Forgetaboudit.

If you want to read the html with javascript so hard, you can do it, but you have to send it via post to your server and mail it from there. Cheerz

ps. try with some test short html content and then with a whole casual webpage

$(document).ready(function(){        
     $("#testlink").click(function() {
      $('#trick').text($("html").html().replace(/[\r\n]+/g, "%0A"));
      window.open('[email protected]?body='+$('#trick').html(), 'email', '');
     }); 
    });
zalew
Can I use this in a bookmark
yesraaj
you mean a bookmarklet? you can load via a bookmarklet whatever you want, it doesn't change the fact that it probably won't work (according to my conclusions). maybe tell us the 'why', this idea is kind of crazy anyway.
zalew