location.href="mailto..." not working?

Forums » Dev.Opera » General Web Development Discussions

You need to be logged in to post in the forums. If you do not have an account, please sign up first.

Go to last post

30. March 2012, 12:28:34

balmac

Posts: 3

location.href="mailto..." not working?

Hi everyone
after tracing back a problem in an application, I have come to such a basic problem that I refrain from thinking that this can actually be the case.
Problem is, it seems that Opera is ignoring any call to location.href if it points to a "mailto" handler if not associated with a link.

Example:
<html>
<script language="Javascript" type="text/javascript">
    location.href='mailto:foobar@domain.tld';
</script>

<body>
    <a href='mailto:foobar@domain.tld'>Mail</a>
</body>
</html>

Expected behaviour is to open a mail on load and on clicking the link "Mail".
While the link works, the call on loading is ignored in Opera.

This works with IE, Firefox, Chrome and Safari.
I have tried the latest Opera (in Linux, Mac OSX and Windows) and various other versions in Windows (11.52, 11.50, 10.60, 10.00) - none works.

Since the link is working I assume the usage of mailto is basically correct. And since the location.href on load is working if changed from mailto: to e.g. http://www.google.ch I have to assume the usage of this is correct too.

This seems so simple I am quite sure I am doing something wrong... or is this actual intended behaviour in some way? Or a bug?
Can anyone confirm this?

Thanks for any insight confused
balmac

30. March 2012, 14:48:44 (edited)

Frenzie

Posts: 14416

Might be some kind of security restriction? Then again, the following works, so I guess it's just a bug:
document.addEventListener('load'), function(){document.getElementsByTagName('a')[0].click()}, false);


That said, I don't really understand. Why not just offer a mailto link in the first place instead of linking to a page that redirects you to a mailto location?

In any case, you don't actually need a script even for that last solution. This will work faster & in more browser configurations, most importantly also with JS disabled:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="0; url=mailto:foobar@domain.tld">
</head>
<body>
<a href='mailto:foobar@domain.tld'>Mail</a>
</body>
</html>



PS Please add a doctype to your code so one doesn't have to test whether it's got something to do with quirksmode, i.e.
<!DOCTYPE html>
<html>
<head>
<script>
document.addEventListener('load'), function(){document.getElementsByTagName('a')[0].click()}, false);
</script>
</head>
<body>
<a href='mailto:foobar@domain.tld'>Mail</a>
</body>
</html>


PPS The expected behavior of your script as given is actually to execute it ASAP, i.e., before load or even DOMContentLoaded.
Intelligent alien life does exist, otherwise they would've contacted us. — CalendarExtend Opera

30. March 2012, 15:47:03

balmac

Posts: 3

Thanks for taking the time to look into this Frenzie.

As to the purpose of this script: This whole thing is solely to demonstrate my problem and has no use on its own. In the actual case this call to location.href is used after some ajax request which provides the information for the mail (recipient, subject, body,...). The call to location.href does not seem to work anywhere in code, it does not have anything to do with being called at load or whenever. While debugging I came to the above conclusion, that location.href with a mailto target is not working at all in Opera (when there is no click event).

Maybe this code demonstrates the problem better (this time including doctype bigsmile ):
<!DOCTYPE html>
<html>
<script language="Javascript" type="text/javascript">

	function setup() {
		document.getElementById("link").onmouseover = function() {
			location.href='mailto:foobar@domain.tld';
		}
		
		document.getElementById("link").onclick = function() {
			location.href='mailto:foobar@domain.tld';
		}
	}
</script>

<body onload="setup()">
    <div id="link" style="background-color: black; color: white; padding: 40px">Mail onhover and onclick</div>
</body>
</html>

This should send a mail when either hovering or clicking the black div. While this works in all other browsers, with Opera only the click event triggers the mail action. Again - changing from mailto: to a URL like http://www.google.ch works for hovering and clicking in Opera too. So there seems to be a problem only with the mailto action.

If there are no other opinions why this might be intended behaviour I guess I will file a bug report.

Kind regards
balmac

31. March 2012, 08:26:18

Frenzie

Posts: 14416

I don't know, opening a mail program on hover? I was a bit in doubt about the location stuff, but this mouseover stuff seems like such a horrible thing that allowing it would frankly sound more like a bug to me. smile

(Then again, it'd probably work if you used the onmouseover = this.click() kind of method I already described, so in that sense it's just random.)
Intelligent alien life does exist, otherwise they would've contacted us. — CalendarExtend Opera

31. March 2012, 08:55:21

balmac

Posts: 3

Frenzie: It has nothing to do with mouseover or whatever. ANY event which is not click does not work (at least it seems like this from my tests, of course I didn't try with all events).
Again: In my real case this is triggered after having received the response to an ajax request. But demonstrating this bug (?) with an ajax request would simply complicate the demonstration.

This is just to show the unexpected behavior, no need to argue if this example is useful or not.
Maybe this works with the workaround of issuing a click(), I will try that. Nonetheless this does not seem like consistent behavior (and all other browsers to get it right).

31. March 2012, 12:27:20

Frenzie

Posts: 14416

Doing it in response to an AJAX event doesn't sound much more expected than doing it on mouseover. Personally I'd be more tempted to say other browsers get it wrong, but I suppose you can call me a user-control radical. p But yes, the strange inconsistency certainly seems a bit buggy.
Intelligent alien life does exist, otherwise they would've contacted us. — CalendarExtend Opera

Forums » Dev.Opera » General Web Development Discussions