I've got a link button which is used to build up a mailto from the contents of the page. What is the best way to launch it from javascript without opening a blank window or disturbing the window it's called from?
function Email()
{
var sMailTo = "mailto:";
var sBody = "";
var alSelectedCheckboxes = new Array();
$("input:checkbox[CheckBoxType=Email]:checked").each(function()
{
alSelectedCheckboxes.push($(this).val());
});
if (alSelectedCheckboxes.length > 0)
{
for (var i=0; i<alSelectedCheckboxes.length; i++)
{
sBody += alSelectedCheckboxes[i];
sBody += "\n";
}
sMailTo += escape("<Insert Recipients Here>") +"?subject=" +escape("<Insert Subject Here>") +"&body=" +escape(sBody);
window.location.href = sMailTo;
}
else
{
alert("Please select some results");
}
}
The simple function is above. window.location.href doesn't work properly unless it's Firefox/Chrome (it redraws the page in IE8). I've also tried window.open(sMailTo, "_self") but again in IE8 this breaks the page that it's called from.
I'm sure this is a stupid question.... :-)
Thanks
I've got a link button which is used to build up a mailto from the contents of the page. What is the best way to launch it from javascript without opening a blank window or disturbing the window it's called from?
function Email()
{
var sMailTo = "mailto:";
var sBody = "";
var alSelectedCheckboxes = new Array();
$("input:checkbox[CheckBoxType=Email]:checked").each(function()
{
alSelectedCheckboxes.push($(this).val());
});
if (alSelectedCheckboxes.length > 0)
{
for (var i=0; i<alSelectedCheckboxes.length; i++)
{
sBody += alSelectedCheckboxes[i];
sBody += "\n";
}
sMailTo += escape("<Insert Recipients Here>") +"?subject=" +escape("<Insert Subject Here>") +"&body=" +escape(sBody);
window.location.href = sMailTo;
}
else
{
alert("Please select some results");
}
}
The simple function is above. window.location.href doesn't work properly unless it's Firefox/Chrome (it redraws the page in IE8). I've also tried window.open(sMailTo, "_self") but again in IE8 this breaks the page that it's called from.
I'm sure this is a stupid question.... :-)
Thanks
Share Improve this question asked Mar 10, 2011 at 20:02 Gordon ThompsonGordon Thompson 4,8348 gold badges50 silver badges63 bronze badges 2- 2 Don't do this quietly on behalf of the user. Add it as a html link. – Raynos Commented Mar 10, 2011 at 20:05
- the javascript is executed by a link. The mailto contents are dynamic however so we need to do a little javascript first to figure out what to send as the body of the email – Gordon Thompson Commented Mar 10, 2011 at 21:06
2 Answers
Reset to default 10I don't think it's stupid at all! This is a good start. What I'd try next is just to create an actual link object in jquery, append it to the body, and then click() it.
//window.location.href = sMailTo;
$('<a href="' + sMailTo + '">click</a>').appendTo('body').click().remove();
Should work as long as the escape() function you're using gets rid of all of the quotes and correctly encodes the html.
But I have to say, this could be hard to get just right. If it still doesn't work, I'd recommend doing it server-side where the link POSTS all of the html needed.
If you're using jQuery, then you can easily create an element based on the markup you build (refer to this answer for an example) and then invoke the link's click event by simply doing this:
element.click();