Escaping ampersand in URL

前端 未结 8 1455
余生分开走
余生分开走 2020-11-22 03:25

I am trying to send a GET message that contains strings with ampersands and can\'t figure how to escape the ampersand in the URL.

Example:

http://www         


        
8条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 03:27

    I would like to add a minor comment on Blender solution.

    You can do the following:

    var link = 'http://example.com?candy_name=' + encodeURIComponent('M&M');
    

    That outputs:

    http://example.com?candy_name=M%26M
    

    The great thing about this it does not only work for & but for any especial character.

    For instance:

    var link = 'http://example.com?candy_name=' + encodeURIComponent('M&M?><')
    

    Outputs:

    "http://example.com?candy_name=M%26M%3F%3E%3C"
    

提交回复
热议问题