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
  • 3,076
  • 7
  • 32
  • 69
  • 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

29

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
  • 312,895
  • 29
  • 395
  • 388
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
  • 732
  • 5
  • 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,755
  • 12
  • 66
  • 130