I have created an html page an used the following javascript to encode url entered in a text box.

However, it does not ecode all characters. I used a search url and tried to encode, but does not encode chars like #,=, etc. How can I encode urls in sharepoint using javascript?

var res = encodeURIComponent(url)
            .replace(/!/g, '%21')
            .replace(/'/g, '%27')
            .replace(/\(/g, '%28')
            .replace(/\)/g, '%29')
            .replace(/\*/g, '%2A')
            .replace(/%20/g, '+');
  • Where are you using this encoded URL?Also you have syntax error near encodeURIComponent(url). – Ganesh Sanap Nov 13 '18 at 9:18
  • it is working now. encoded URL is not go as it does not encode everything. My issue was that the changes were not properly applied to the page. – Burre Ifort Nov 13 '18 at 9:54
  • you can add the solution and close this question. – Ganesh Sanap Nov 13 '18 at 9:59

the issue actually was with my file as it was not properly uploaded/saved. I had opened the html file via the explorer in Notepad++ and when I saved the file, I thought it was updated directly in SharePoint as is most of the time the case. However, in this case it was not. I pressed F12, checked my JavaScript code and I found out that the file was not updated. Then I dragged and dropped the file inside the library and everything started working fine.

A very good post about encodeUri and encodeURIcomponent can be found here: https://stackoverflow.com/questions/75980/when-are-you-supposed-to-use-escape-instead-of-encodeuri-encodeuricomponent

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.