Skip to main content
November 25, 2011
Answered

Special Character Handling

  • November 25, 2011
  • 1 reply
  • 1398 views

Hi,

I'm using the ColdFusion.navigate in JavaScript to call Method in CFC.

ColdFusion.navigate('cfc/data_control.cfc?method=insertCompanyName&company_id='+document.getElementById('company_id_'+i).value+'&company_name='+document.getElementById('company_name_'+i).innerText,'addMessage',loadCompanyList);

However, I find that when I input the name "Testing & Checking" in the field, it will only show me the word "Testing ". I think this is because of the character "&". "&" is used to divide different parameter. Since some companies have the character "&", I want to find some method to retrieve the character "&" correctly.

Is there any method on handling "&" correctly?

    This topic has been closed for replies.
    Correct answer Adam Cameron.

    I know the above answer has been marked as "correct", but I'm afraid it's not the best way of going about things, nor is it complete.

    There are more characters the user could potentially enter which will cause problems here than just ampersand.  Fortunately this possibility was predicted by the JavaScript bods, so there's a function to deal with it: encodeURIComponent():

    http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp

    That is what should be being used in these situations.

    --

    Adam

    1 reply

    Inspiring
    November 28, 2011

    You'll need to use a JavaScript string replace() method (or a similar equivalent) in order to do that since your code is being executed client side rather than server side.  Replace it with its corresponding URL encoded value (%26)

    Adam Cameron.Correct answer
    Inspiring
    November 28, 2011

    I know the above answer has been marked as "correct", but I'm afraid it's not the best way of going about things, nor is it complete.

    There are more characters the user could potentially enter which will cause problems here than just ampersand.  Fortunately this possibility was predicted by the JavaScript bods, so there's a function to deal with it: encodeURIComponent():

    http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp

    That is what should be being used in these situations.

    --

    Adam