-2

I am working on a website to have it be able to use mailto without typing out the email in the script. here is the function

<script type= "text/javascript"> 
    function createemail(name,subject) 
      {
      var email = name + '@email.com';
      var mailto_link = 'mailto:' + email + '?subject=' + subject;
      win = window.open (mailto_link, 'emailWindow');
      //if (window && window.open && !window.closed) window.close()----DOES NOT CLOSE WINDOW };
  </script>

I then reference the function with a onclick command

   <tr>
  <td align="center" valign="middle">
    <font size="+1"><a onclick = "createemail('TEST','Subject Text')"; style= "cursor:pointer; cursor:hand; color:#0000ee"><u>Sales</u></a></font>
  </td>
</tr>

The text is changed to look like a hyperlink but it just references the function

Yaakov Ainspan
  • 3,598
  • 3
  • 23
  • 45
ineedalife
  • 85
  • 9

3 Answers3

0

I'm not entirely sure what you are trying to do, but if I am correct, you're trying to close the window after the email window has been opened.

You can't do that.

Javascript can only close a window that has been opened by it. See this question for more information.

P.S. Really, why are you using font?

Yaakov Ainspan
  • 3,598
  • 3
  • 23
  • 45
0

Well, if you want the behavior of that anchor tag to open up your email client (Outlook or whatever) then you have to change the innerHTML of the anchor tag with your mailto_link. You have to set an id for that anchor tag and by using the document.getElementById('anchorTagId') you can then later set it.

function createemail(name,subject) 
{
var email = name + '@email.com';
var mailto_link = 'mailto:' + email + '?subject=' + subject;
win = window.open (mailto_link, 'emailWindow');
//if (window && window.open && !window.closed) window.close()----DOES NOT CLOSE WINDOW
var _theEmail = document.getElementById('theEmail');
_theEmail.innerText = mailto_link;
};


<table>
    <tr>
        <td align="center" valign="middle">
          <font size="+1"><a id="theEmail" onclick = "createemail('TEST','Subject Text')"; style= "cursor:pointer; cursor:hand; color:#0000ee"><u>Sales</u></a></font>
        </td>
      </tr>
</table>
0

I changed the Function to

  <script type= "text/javascript"> 
function createemail(name,subject) 
  {
  var email = name + '@bldgcs.com';
  location.href = 'mailto:' + email +'?subject=' + subject;
  };

The location.href seemed to fix the problem

ineedalife
  • 85
  • 9