12
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?

1
  • 1
    Use encodeURIComponent to avoid breaking changes in the future. Commented Jul 18, 2013 at 21:37

3 Answers 3

30

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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}`;

Comments

4
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

1 Comment

This answer should be in the comments of the original answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.