How to ask permision so others can open mailto - javascript

I'm trying to open mail on click in js. I'm using mailto, but in chrome you need to change the handlers settings to make it work.
My question is can I make some sort of popup that ask for persmission and changes this setting to others?
PS: to give permision for me I only found this way:
Open Gmail in Chrome and click the Protocol Handler icon overlapping-diamonds in your browser's address bar.
When prompted to "Allow Gmail to open all email links?", select "Use Gmail," and click "Done."
EDIT
My code to send the mail
$("#applyText").click(function(){
var email = 'mail#gmail.com';
var subject = 'Hire me im a genius';
var emailBody = 'Hi Sample,';
document.location = "mailto:"+email+"?
subject="+subject+"&body="+emailBody;
});

this is browser settings specific, ie. it will behave differently depending on the user's browser settings. The user can change how mailto: links behave in chrome by visiting chrome://settings/handlers, or Chrome Settings->Content Settings->Manage Handlers...
This is an answer to that question.
So to answer your question
No it is not possible because this is browser settings.
But most browsers should open it without a problem. For me this works properly.
By the way, you have some minor mistakes in your code.
Here is an working example
$("#applyText").click(function() {
var email = 'mail#gmail.com';
var subject = 'Hire me im a genius';
var emailBody = 'Hi Sample,';
document.location = "mailto:" + email + "?subject=" + subject + "&body=" + emailBody;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="applyText">Click</button>

Related

Twitter intent link not working on page, but it does work from dev tools

When I click on the link on the page, I receive the following error:
twitter.com is blocked twitter.com refused to connect ERR_BLOCKED_BY_RESPONSE
I use the following code to add the link to the page:
function getQuote() {
let newQuote = randomQuote().q;
let newAuthor = randomQuote().a;
let tweetURL = 'https://twitter.com/intent/tweet?hashtags=quotes&text=';
tweetURL = tweetURL + encodeURIComponent(newQuote + ' ' + newAuthor);
$('#text').text(newQuote);
$('#author').text(newAuthor);
$('#tweet-quote').attr('href', tweetURL);
}
If I open up the developer tools in chrome, I can click on the link in the tag and it works perfectly fine.
Why won't it work on the page?
I had the same issue with my React code. I also had target attribute set to "_blank".
Read the below post, changed it to "_top" and the tweet button started working on my chrome browser.
https://github.com/freeCodeCamp/freeCodeCamp/issues/40089

Excluding tel and mailto in base target statement

Per a client request, we forced all external links to open in a new tab. Currently I am using a js script for that.
However, it is also forcing the tel and mailto links to open new tabs as well. And I think that is why I am getting an error on some devices when you click and email link, where a pop up box says "This website has been blocked from automatically composing an email" - that one was uncovered specifically on an iPhone 6.
How can I force external links- but exclude mail and telephone links? Any assistance is greatly appreciated.
This is the external link script that I am using currently. I thought maybe I could just make an alteration to look for only external links that had "http" but an else statement for tel and mailto. But I am not sure how I could go about that.
jQuery(document).ready(function($) {
$('a').each(function() {
var a = new RegExp('/' + window.location.host + '/');
if(!a.test(this.href)) {
$(this).click(function(event) {
event.preventDefault();
event.stopPropagation();
window.open(this.href, '_blank');
});
}
});
});
Combined with my comment, I would opt to go for a check against the href attribute of each anchor, and check that it does not start with mailto: or tel::
(and the external link is not working at all, I guess that is caused by the snippet environment)
jQuery(document).ready(function($) {
$('a').each(function() {
var a = new RegExp('/' + window.location.host + '/');
if(!a.test(this.href) && this.href.substr(0,4)!='tel:' && this.href.substr(0,7)!='mailto:') {
$(this).attr('target', '_blank');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
website,
e-mail and
phone.
I must admit that it is not ideal to check for all protocols that you might want to exclude, but it definitely is easier than checking for everything that you would like to open in a new window/tab.

How to change browser tab displayed text in JSFiddle

I'm trying to implement a notification functionality which would change the browser tab text with the number of notifications. I searched on the internet and found that I should change the title which would change the browser tab text too . But it doesn't seem to work. Here is what I tried:
var nbNotifications = 0;
$().ready(function() {
var docOriginalTitle = "Tab does not change on notification";
debugger;
document.title = docOriginalTitle;
$('#test').click(function() {
nbNotifications += 1;
document.title = "(" + nbNotifications + ") " + docOriginalTitle;
alert(document.title);
});
});
JSFiddle can be found here
Perhaps, it's not true that the document title changes the browser tab title? Or maybe it doesn't work with JSFiddle?
Could you put me on the good track?
Edit
I'm trying to change the browser tab text in JSFiddle. Is there a way to cheat JSFiddle and render this behaviour?
I think this technique works just fine. I think the problem is that jsFiddle runs your page in an iframe, which does not control the browser tab title.
If I enter document.title = 'Here's my new Title'; in the browser console, the text in the browser tab does update.
How else have you tested this?
document.title is the way to go, but it won't work in jsFiddle. It should work fine in a "real" page.
Try typing javascript:alert(document.title = "My new title"); into your browser - you should see the tab text update.
Edit:
I don't think you can do this in jsfiddle unless they allow for it somehow. If you try to "escape" your window and change properties on theirs (i.e. parent.document.title = "xxx"; you get a security exception:
Uncaught SecurityError: Blocked a frame with origin
"http://fiddle.jshell.net" from accessing a frame with origin
"http://jsfiddle.net". Protocols, domains, and ports must match.

Javascript : page loses focus after opening mail client

Help!
I have an iPad app which runs html5 pages... one of the pages requires an email to be sent which opens the Mail program using this code
var link = 'mailto:' + toEmailAdd +'?subject=PDFs&body='+htmlString
window.open(link, 'Mailer');
After I send the email and go back to the program the page has lost focus and has the loading circle running in front of it.
Any help on how to make sure the page doesn't lose focus would be hugely appreciated!
I managed to work this out...
$('#addBtn').bind( "click", function() {
var link = 'mailto:' + toEmailAdd +'?subject=PDFs&body='+htmlString
window.open(link, 'Mailer');
setTimeout(ResetPage, 1);
});
function ResetPage(){
document.location = "";
};

Automatically open default email client and pre-populate content

I need to automatically open a user's default email client when they save some content on a page. I need to populate the email subject, to address, and put some content in the email body.
What is the best option to achieve this?
I'm aware of the mailto: attribute, but the user must click on this and I'm not sure it allows you to specifiy the subject and content?
As described by RFC 6068, mailto allows you to specify subject and body, as well as cc fields. For example:
mailto:username#example.com?subject=Subject&body=message%20goes%20here
User doesn't need to click a link if you force it to be opened with JavaScript
window.location.href = "mailto:user#example.com?subject=Subject&body=message%20goes%20here";
Be aware that there is no single, standard way in which browsers/email clients handle mailto links (e.g. subject and body fields may be discarded without a warning). Also there is a risk that popup and ad blockers, anti-virus software etc. may silently block forced opening of mailto links.
JQuery:
$(function () {
$('.SendEmail').click(function (event) {
var email = 'sample#gmail.com';
var subject = 'Test';
var emailBody = 'Hi Sample,';
var attach = 'path';
document.location = "mailto:"+email+"?subject="+subject+"&body="+emailBody+
"?attach="+attach;
});
});
HTML:
<button class="SendEmail">Send Email</button>
Implemented this way without using Jquery:
<button class="emailReplyButton" onClick="sendEmail(message)">Reply</button>
sendEmail(message) {
var email = message.emailId;
var subject = message.subject;
var emailBody = 'Hi '+message.from;
document.location = "mailto:"+email+"?subject="+subject+"&body="+emailBody;
}
Try this:
It will open the default mail directly.
<img src="ICON2.png">

Categories