11
var a = 1;
var b = 2;
var mylink = "http://website.com/page.aspx?list=' + a + '&sublist=' + b + '";

This doesn't work. Is there a simple way to insert these other variables into the url query?

user1447679
  • 2,936
  • 6
  • 28
  • 63
  • 1
    Use [encodeURIComponent](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) to avoid breaking changes in the future. – user2246674 Jul 18 '13 at 21:37

3 Answers3

28

By using the right quotes:

var a = 1;
var b = 2;
var mylink = "http://website.com/page.aspx?list=" + a + "&sublist=" + b;

If you start a string with doublequotes, it can be ended with doublequotes and can contain singlequotes, same goes for the other way around.

adeneo
  • 303,455
  • 27
  • 380
  • 377
3

In modern JavaScript standards we are using ${var} :

var a = 1;
var b = 2;
var mylink = `http://website.com/page.aspx?list=${a}&sublist=${b}`;
Skotee
  • 554
  • 4
  • 7
3
var a = 1;
var b = 2;
var mylink = `http://website.com/page.aspx?list=${a}&sublist=${b}`;

Copied from above answer.

Do notice that it is not single quote.

enter image description here

Vineesh TP
  • 7,389
  • 10
  • 58
  • 118