mailto fails in IE where there is a long body text. Is there any way to resolve this?

I never could get the location.href = mailtoHref hack to work. However, I have found that following works.

$('body').append($('<iframe id="mailtoHack" src="' + mailtoHref + '"/>');
$('#mailtoHack').remove();

EDIT

Here is a way to do it without jQuery:

function mailtoHack(href) {
    var iframeHack;
    if (href.indexOf("mailto:") === 0) {
        iframeHack = document.createElement("IFRAME");
        iframeHack.src = href;
        document.body.appendChild(iframeHack);
        document.body.removeChild(iframeHack);
    }
}

And, for good measure, here is a Knockout custom binding usable as data-bind="mailto: foo":

ko.bindingHandlers.mailto = {
    init: function (element, valueAccessor) {
        ko.utils.registerEventHandler(element, "click", function (e) {
            var href = ko.unwrap(valueAccessor()), iframeHack;
            if (href.indexOf("mailto:") === 0) {
                iframeHack = document.createElement("IFRAME");
                document.body.appendChild(iframeHack);
                document.body.removeChild(iframeHack);
            } else {
                e.preventDefault();
            }
        });
    }
};

Leave a Comment