To encode the url in JavaScript, following javascript built in functions can be used.
1.
encodeURIComponent(string) - to encode querystring parts only
<script>
var url = "http://www.dotnetfunda.com" + encodeURIComponent('variables in C# artilces');
</script>
2.
encodeURI(string) - to encode complete url
<script>
var url = "http://www.dotnetfunda.com" + encodeURI('variables in C# artilces');
</script>
Thanks