Search code examples
javascriptasciiquery-stringpixelencode

Javascript: how to write a tracking pixel with a query string


I need to put a tracking pixel into my html page. I'm using this snippet:

<script type='text/javascript'>
    var dataString="a=10&b=19";
    var track = new Image();
    track.src="http://pixel.tag.com/event/im.gif?" + dataString;
</script>

Why do I not need to encode the string contained in dataString? It could contain non ASCII character. Is there any reason?

Thanks, Alessio


Solution

  • in your case the encoded string will be the same as the given one. this is why you don't have any issues with it.

    'a=10&b=19' === encodeURI('a=10&b=19')
    

    to keep the application flexible for future changes better encode your queryString.