Javascript location href mailto

Javascript could do this for you. By using the «document.location.href». Or, if you are using .php you can do it even easier. Can you use php? let me know and I can make an example for you!

4 Answers 4

This is the pure javascript solution:

This might cause the page to change. Return false to prevent that. . + window.location;return false;»> .

This opens the email client in the current window itself. Any way I can make it open in a new tab? Thanks.

With Javascript, you utf-8-percent-encode the subject and body hfvalues using encodeURIComponent() on a UTF-8 page.

       

Email link to this page

If you’re doing this server-side, you can just build the mailto link and emit it as the href attribute value. Then, you won’t need JS at all.

I assume ASP has some URI encoding functions that work like encodeURIComponent().

You can also view the source of my mailto URI composer page as another example.

For the < and >that I encase the URI in, in the JS code above, see «Appendix C. Delimiting a URI in Context» of RFC3986 for why.

Also, instead of window.location.href, you can use window.location or document.location.href or document.location. I normally use «document.location».

For why you use a Javascript URI instead of an onclick attribute, see this answer.

Also note that in the JS URI in the code above, I wrapped the code in an anonymous function. That’s not necessary in this case because the function doesn’t return anything that would change the document when you click. But, it’s to just do it all the time for good measure.

See my Javascript URI compose to help with creating javascript URIs.

Источник

Window location.href

The location.href property sets or returns the entire URL of the current page.

Syntax

Property Value

Parameter Description
URL An absolute URL like:
http://www.example.com/default.htm

A relative URL like
default.htm

An anchor URL like
location.href=»#top»

Return Value

More Examples

Set the href value to point to an anchor within a page:

Set the href value to point to an email address (will open and create a new email message):

Browser Support

location.href is supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

I’d like to invoke a mailto link from JavaScript — that is I’d like a method that allows me to open the email client on the users PC, exactly as if they had clicked on a normal mailto link. How can I do this?

6 Answers 6

You can use window.location.href here, like this:

window.location.href = "mailto:address@dmail.com"; 

and how would I append a body? mailto:address@dmail.com?body=myBody and mailto:address@dmail.com&myBody do not work for me.

@Adnan from what I remember it doesn’t work with Opera and maybe more browsers. Depending on your needs, you might be able to workaround that problem.

Max, you have likely figured this out, but for future readers: window.location.href = ‘mailto:address@dmail.com&subject=Hello there&body=This is the body’; . Not ? and not & , just &

You can avoid the blank page issue discussed above by instead using .click() with a link on the page:

document.getElementById('mymailto').click(); . 

the working answer for me, tested in chrome, IE and firefox together with outlook was this

window.location.href = 'mailto:address@dmail.com?subject=Hello there&body=This is the body'; 

%0d%0a is the new line symbol of the email body in a mailto link

%20 is the space symbol that should be used, but it worked for me as well with normal space

window.open(‘mailto:address@mail.com?subject=sub&body=this is body’)

If we use window.location.href chrome browser is having error in network tab with Provisional headers are shown Upgrade-Insecure-Requests: 1

Actually, there is a possibillity to avoid the empty page.

I found out, you can simply insert an iframe with the mailto link into the dom. This works on current Firefox/Chrome and IE (also IE will display a short confirm dialog).

var initMailtoButton = function() '); var button = $('#mailtoMessageSend'); if (button.length > 0) < button.click(function()< // create the iframe $('body').append(iframe); //remove the iframe, we don't need it any more window.setTimeout(function()< iframe.remove(); >, 500); >); > > 

This is an old question, but I combined several Stack Overflows to come up with this function:

//this.MailTo is an array of Email addresses //this.MailSubject is a free text (unsafe) Subject text input //this.MailBody is a free text (unsafe) Body input (textarea) //SpawnMail will URL encode /n, ", and ', append an anchor element with the mailto, and click it to spawn the mail in the users default mail program SpawnMail: function()< $("#MyMailTo").remove(); var MailList=""; for(i in this.MailTo) MailList+=this.MailTo[i]+";"; var NewSubject=this.MailSubject.replace(/\n/g, "%0d%0a"); NewSubject=NewSubject.replace(/"/g, "%22"); NewSubject=NewSubject.replace(/'/g, "%27"); var NewBody=this.MailBody.replace(/\n/g, "%0d%0a"); NewBody=NewBody.replace(/"/g, "%22"); NewBody=NewBody.replace(/'/g, "%27"); $("#mainNavBar").after(" "); document.getElementById('MyMailTo').click(); > 

The cool part about this (and how I plan to use it) is I can put this in a loop and split out individual messages to everyone in the array or keep everyone together (which is what this function currently does). Anyway thanks for the tip @Toskan

Источник

mailto using javascript

I want to open a new outlook mail template with the ‘To address’ whenever a user clicks an image. I have return my code in a html page(linked with the image), whenever it loads the javascript should open a new mail template. But the functionality is not working. Kindly let me know what is wrong in my code.

body onLoad="redirect()" script language="JavaScript" function redirect() var email = "xyz@something.com" var mailto_link = 'mailto:' + email window = window.open(mailto_link, 'emailWindow') if (window && window.open && !window.closed) window.close() 

7 Answers 7

No need for jQuery. And it isn’t necessary to open a new window. Protocols which doesn’t return HTTP data to the browser ( mailto: , irc:// , magnet: , ftp:// (

It should be noted, that the browser will still trigger the onbeforeunload -Event: window.onbeforeunload = () => ; window.location.href = «mailto:mail@example.org»;

@DB Thanks for pointing this interesting detail out! This makes sense. I guess, because, like I previously said, the browser might have a build-in tool for handling pseudo-protocols, like ftp://. This would indeed replace the current page contents, so the onbeforeunload event is fired, even if there is no built-in tool in the browser to handle mailto:// URIs. But this is just a personal assumption. In this case, the browser vendors should check first, if they support the pseudo-protocol, otherwise not firing the onbeforeunload event.

@cjm2671 I tested the above code in the current newest Chrome version 76.0.3809.100 in normal mode and incognito mode. Everything works fine. Do you have any browser extensions (AddOns), which might prevent the code execution?

Источник

How can I launch the eMail client, and then do a page redirect with Javascript?

I’m required to make a website function exactly the same on other browsers as it does in IE6. Part of the current code looks similar to this:

in IE, this causes the mail client to open with the specified message prepared, and then redirects the browser to newPage.html. Other browsers, however, only redirect to newPage.html. How can I achieve this effect (opening the mail client and then doing a page redirect) consistently across browsers?
As a note, I’ve also tried to accomplish this using meta refresh, but was unsuccessful.

I’ve been testing in Chrome. It doesn’t seem to be working in Firefox 3.5.1 for me on this machine, though, either.

5 Answers 5

On now that’s smart. But I’d include a slight delay before changing the window location in the onclick handler.

Generally speaking, I actually much prefer this solution, but in this specific case I think it’s better for me to stick with the javascript based solution.

Changing the href property will start a location load, changing it again afterwards will cancel the previous navigation.

It appears that IE6 will start the e-mail client immediately upon setting the property, then continue the javascript execution. Other browsers appear to do things differently, and the second location load will cancel the first.

I managed to work around this in Chrome with a timer, it might work for other browsers too:

Stop whining, I beat you both by 4 minutes! 🙂 On the other hand, @Andy E you took the time to actually test it, so the mark went to the right person.

@T.R.: And you chose the right one. @Pekka: Oh, I agree (about Andy), I’m just joking around. 🙂 (But for the record: I’m pretty sure I posted my answer before you edited yours.)

@Pekka: You beat my answer with your edit, so it must have been really close. I have questions sorted in order of oldest so that I mark the first correct one but I supposed that doesn’t always work with edits. @T.J. Crowder: I knew you were joking 😉

On the whole, I tend to think security settings will get in your way and would recommend just giving the user a boring old-fashioned mailto link to click. (Edit: Perhaps one set up like Mic suggests.)

That said, I wonder if things become any more reliable if you introduce a delay:

This will work only if the client’s browser knows which E-Mail client to open for mailto: links in the first place. If the user uses a web-based client that is not registered with the browser, nothing will happen.

Also, it could be that security settings prevent mailto: links from opening programmatically, or will prevent it in the future.

I wouldn’t rely on this to work either way, only as a nice optional convenience function.

Anyway, to answer your question, can you try setting a timeout between the two calls? Maybe the location refresh is just too quick for the browser to catch up.

location.href="mailto:test@test.com&body=Hello!"; setTimeout(function()< location.href = 'newPage.html' >, 500); 

Источник

Читайте также:  Geek for geek java
Оцените статью