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 902765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:46:16+00:00 2026-05-15T15:46:16+00:00

Seems like a very simple question here: I’ve got a program where someone is

  • 0

Seems like a very simple question here:

I’ve got a program where someone is entering a string M&S in a form and running a query. I understand the & is a reserved character and therefore must be encoded. The problem is it seems to be requiring encoding twice in some contexts.

If the URL is used in a javascript onClick event, normal URL encoding seems to work fine (here the operator can click on a column header to sort):

<td onClick="AJAX_Get('http://10.0.0.195/program.exe?Qry147=M%26S&sortmethod1=161')">

However, if the URL is used in an anchor (although the anchor actually uses AJAX), it seems to need encoding twice:

<a href="javascript:AJAX_Get('http://10.0.0.195/program.exe?Qry147=M%2526S&sortmethod1=147')" title='Refresh'>Refresh</a>

Both of the examples above work fine. However they are hand-generated test cases. Unfortunately, in the application, when I’m actually generating the URL, I don’t know how it’s going to be used.

If I encode the URL parameter once (M%26S), it works fine in onClick. But used this way in the anchor, the server sees the URL as ...Qry147=M&S&sortmethod1=147... – so it must have been unencoded before being given back to the server.

If I encode it twice (M%2526S), the anchor works, but for the onClick, the server sees ...Qry147=M%2526S....

I get the feeling I’m missing something here. Is there a way to make this work the same in both cases?

  • 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-15T15:46:17+00:00Added an answer on May 15, 2026 at 3:46 pm

    If you construct the whole HTML text using simple steps, some of the difficulties will disappear. So, by taking your example, you want to encode the following query parameter:

    M&S
    

    When you embed this string as a query parameter in a URL, you have to urlencode it, as you already know. The urlencoded string is M%26S. The complete URL then looks like this:

    http://10.0.0.195/program.exe?Qry147=M%26S&sortmethod1=147
    

    Now this URL is embedded in JavaScript code, and in this case you only need single quotes at both ends:

    'http://10.0.0.195/program.exe?Qry147=M%26S&sortmethod1=147'
    

    The whole JavaScript code looks like this:

    AJAX_Get('http://10.0.0.195/program.exe?Qry147=M%2526S&sortmethod1=147')
    

    Now this whole text is used in an HTML context that is interpreted as a URL, so you need to urlencode it again:

    AJAX_Get('http://10.0.0.195/program.exe?Qry147=M%2526S&sortmethod1=147')
    

    And finally, since you are embedding this text in HTML, you need to htmlescape it:

    AJAX_Get('http://10.0.0.195/program.exe?Qry147=M%2526S&amp;sortmethod1=147')
    

    That’s why you end with:

    <a href="javascript:AJAX_Get('http://10.0.0.195/program.exe?Qry147=M%2526S&amp;sortmethod1=147')" title='Refresh'>Refresh</a>
    

    I usually avoid these encoding challenges by not putting the string M&S directly into the onclick event or the anchor. In general, you cannot encode the onclick event and the anchor in the same way, since the decoding process for both is different:

    onclick: html -> js -> url
    anchor: html -> url -> js -> url
    

    But wait … if you write a helper function like this, it works:

    function myQuery(q) {
        var encodedQ = encodeURIComponent(q); // TODO: which character encoding is used here?
        var url = 'http://10.0.0.195/program.exe?Qry147=' + encodedQ + '&sortmethod1=147';
        var response = AJAX_Get(url);
        // TODO: handle errors
    }
    

    Now you can write:

    <a href="javascript:myQuery('M&amp;S')">anchor</a>
    <a onclick="myQuery('M&amp;S')">event</a>
    

    This trick works because there is no % anymore in the anchor case.

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

Sidebar

Ask A Question

Stats

  • Questions 445k
  • Answers 445k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer <%= form.input :q, :input_html => { :name => 'q' }… May 15, 2026 at 7:03 pm
  • Editorial Team
    Editorial Team added an answer Certainly the "Single Responsibility Principle" or something near it and… May 15, 2026 at 7:03 pm
  • Editorial Team
    Editorial Team added an answer List<string> sqlSortingList = new List<string>(); if (OrderList != null) {… May 15, 2026 at 7:03 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.