Search code examples
javascriptfirefoxgoogle-chromemailto

Firefox and IE won't call window.location.href if value is in a variable


I am trying to call a mailto: URI which is stored in a variable. When I do window.location.href = mailto_link; Firefox gives me the following error:

NS_ERROR_ILLEGAL_VALUE: Component returned failure code: 0x80070057        
(NS_ERROR_ILLEGAL_VALUE) [nsIDOMLocation.href] 

window.location.href = mailto_link;` 

What IE says:

Object doesn't support this property or method

The code works in Chrome but not in IE nor Firefox.

my original function:

function email()
{
    var nom = $('#nom').val();nom = encodeURIComponent(nom);
    var compagnie = $('#compagnie').val();compagnie = encodeURIComponent(compagnie);
    var rue = $('#rue').val();rue = encodeURIComponent(rue);
    var ville = $('#ville').val();ville = encodeURIComponent(ville);
    var province = $('#province').val();province = encodeURIComponent(province);
    var cp = $('#cp').val();cp = encodeURIComponent(cp);
    var remarques = $('#remarques').val();if(remarques ==""){remarques = "Aucune remarque.";}remarques = encodeURIComponent(remarques);
    var quantite = $('#quantite').val(); 
    var email= "[email protected]";
    var subject= "Nouvelle commande";
    var body_message= "%0D%0D%0D%0D"+nom+"%0D"+compagnie+"%0D"+rue+"%0D"+ville+", "+province+"%0D"+cp+"%0D%0D%0DRemarques:"+remarques+"%0D%0D Quantit%E9:"+quantite;
    var mailto_link = 'mailto:'+email+'?subject='+subject+'&body='+body_message;
    window.location.href = mailto_link;
}

UPDATE 1

I found out what was causing the issue for IE, although I am still looking to resolve it for Firefox. The problem for IE was that I had a console.log(); which wouldn't be recognized (IE8 and lower versions).

Here is a console.log() of the content of mailto_link:

mailto:[email protected]?subject=Nouvelle commande&body=Charger %0Dmodems des %CEseulement%0D%0D%0D%0Djshad%0Daskjda%0Daskdj%0Daskdj, askdj%0DJ9P%204A1%0D%0D%0DRemarques:asldk%0D%0D Quantit%E9:14 

Solution

  • Firefox is apparently unable to handle the ISO 8859-1 characters above 128 in URLs. The problem disappears if you remove the %CE(Î) and %E9(é) from your logged example. Unfortunately, the only workaround I can think of requires manually replacing extended characters like those with an equivalent (perhaps HTMLEntities*). Since there is no native function to do so in Javascript, that could get quite annoying.

    **Because HTMLEntities are only rendered properly in an HTML context, and mailto: URIs produce a plain-text message body, this is an imperfect solution. Below is a function that will do this, but the message will have instances of é and the like. Perhaps a more convenient solution is to convert accented characters to their equivalents in the first 128 ASCII characters as you mentioned in the comments.*


    function encodeISO8859 (str) {
        var rstr="";
        for(var i=0; i<str.length; i++) {
            var c = str.charCodeAt(i);
            if(c>191&&c<=255&&!(c==215||c==247)){
                console.log(c);
                rstr += "&#"+c+";";
            } else {
                rstr += str.charAt(i);
            }
        }
        return rstr;
    }
    

    This will turn any character in the ISO8859-1 character set (see bottom of page) into its equivalent HTMLEntity. Use this BEFORE encoding for URI:

    var nom = $('#nom').val();nom = encodeURIComponent(encodeISO8859(nom));
    

    Of course, only do this if the accented characters are absolutely necessary for comprehension, and there's likely to be overlap between many accents that use the same base character (like A).