简体   繁体   中英

Cannot open page in a new tab using location.href

I need to use window.location.href to open the user's email so for that I have the following code:

$("#email").click(function(){
      window.location.href = "mailto:myemail.com?subject=Subject&body=xxxxxxxxxxxxxxxxxxxxxxxxxxx";

});

Which works fine. However, the problem is I cannot open the user email page in a new tab and it opens by redirecting the current page which is not user friendly. How can I achieve this?

You're looking for window.open()

Example :

window.open("mailto:myemail.com?subject=Subject&body=xxxxxxxxxxxxxxxxxxxxxxxxxxx");

You can use the target="_blank" attribute on any regular anchor tag to make sure that the link is opened in a new tab, without using any JavaScript. Note, however, that mailto links are often handled by browsers specially and may just redirect to the system's default mail app without opening a visible tab.

<a id="email" target="_blank" href="mailto:person@example.com?subject=Subject&body=Body">
  Email us!
</a>

Edit: It looks as if this method won't work in certain versions of Firefox, due to a bug. (See this answer .)

Have a look at this: Open the href mailto link in new tab / window

You can't open a 'mailto' link in new tab/window by default.

You need to use window.open() in your click override.

please check below link and you can view the demo using this link.

https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM