MailTo从Javascript [英] MailTo From Javascript

查看:78
本文介绍了MailTo从Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接按钮,该按钮用于从页面内容构建mailto.从javascript启动它而不打开空白窗口或不打扰其调用窗口的最佳方法是什么?

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");
    }
}

上面是简单的功能.除非是Firefox/Chrome,否则window.location.href无法正常工作(它会在IE8中重新绘制页面).我也尝试过window.open(sMailTo,"_self"),但是在IE8中,这又再次打断了它的调用页面.

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.... :-)

谢谢

推荐答案

我不认为这完全愚蠢!这是一个好的开始.我接下来要尝试的只是在jquery中创建一个实际的链接对象,将其附加到主体,然后单击()它.

I 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();

只要您使用的escape()函数可以消除所有引号并正确编码html,就应该可以工作.

Should work as long as the escape() function you're using gets rid of all of the quotes and correctly encodes the html.

但是我不得不说,这很难使 just 正确.如果仍然无法正常工作,我建议在服务器端将链接发布所有需要的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.

这篇关于MailTo从Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆