Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6779255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:21:49+00:00 2026-05-26T16:21:49+00:00

I have a C# ASP.NET app that creates a JavaScript array of values for

  • 0

I have a C# ASP.NET app that creates a JavaScript array of values for some user profile information. Client-side, I use jQuery/JavaScript to read the array and generate a mailto link. Some of the fields can contain special characters, such as ' & = / \ ".

Here’s the C# code:

private String generateElementsArray(){
  StringBuilder sb = new StringBuilder();
  sb.Append("var currentElements = { currentUserName: '");
  sb.Append(currentUserName);
  sb.Append("', currentUserEmail: '");
  sb.Append(currentUserEmail);
  sb.Append("', currentSite: '");
  sb.Append(currentSite);
  sb.Append("', currentTitle: '");
  sb.Append(currentTitle);
  sb.Append("'}");
  return sb.ToString();
}    

I write the value of the above method to the page, which produces this JavaScript:

<script type="text/javascript">var currentElements = { currentUserName: 'Alex', currentUserEmail: 'myemailID', currentSite: 'Helpdesk', currentTitle: 'Phone User Guides & Troubleshooting'}</script>

Then I generate the email link using this JavaScript code, attaching the anchor tag to an element on the page:

function generateEmailTo(){
  var body = currentElements.currentUserName + ' has shared a page with you on the intranet.%0A%0APage Title: %22' +
    currentElements.currentTitle.replace("&","and")  + '%22%0A' + $(location).attr('href').replace('#',''); 
  var subject = currentElements.currentUserName + ' has shared a page with you on the intranet';
  var mailto = 'mailto: ?body=' + body + '&subject=' + subject;
  var anchor = '<a href="' + mailto + '"></a>';

  $("#email-placeholder").wrap(anchor);
}
....

<img id="email-placeholder" title="Send this page to a friend." src="<%= baseUrl %>/SiteAssets/media/icons/email-icon.gif"/>

For the mailto body text, I’ve noticed that it only takes a small set of the encoded characters, such as %22 for double-quotes, and %0A for line breaks. How do I pass the special characters such as single quotes, ampersands, etc., to the mailto body text and keep JavaScript happy?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:21:49+00:00Added an answer on May 26, 2026 at 4:21 pm

    Mailto is a URI scheme so all of its components must be URI encoded. You can use JavaScript encodeURIComponent function to encode mailto components. Please see the following example:

    function buildMailTo(address, subject, body) {
        var strMail = 'mailto:' + encodeURIComponent(address)
                       + '?subject=' + encodeURIComponent(subject)
                       + '&body=' + encodeURIComponent(body);
        return strMail;
    }
    var strTest = buildMailTo('abc@xyz.com', 'Foo&foo', 'Bar\nBar');
    /* strTest should be "mailto:abc%40xyz.com?subject=Foo%26foo&body=Bar%0ABar" */
    window.open(strTest);
    

    Hope this help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET app that sits on our intranet, using the WindowsIdentity to
I have an asp.net app that uses System.IO.Path.GetTempFileName() for temporary files. In the production
I have an ASP.NET MVC app that opens a Request view in a new
I have an asp.net mvc app that is built to run as standard web
Assume I have an ASP.NET MVC app that's not doing anything too fancy (no
So, I have an asp.net mvc app that is being worked on by multiple
I have a asp.net web forms app that uses System.Web.Caching.Cache to cache xml data
I have an ASP.NET app running in IIS that is hosting files all great
We have an ASP.NET 2.0 WebForms app that uses MS Ajax 1.0. It's working
I have a SQL query in my ASP.net web app that looks like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.