How to insert javascript variables into a URL

后端 未结 3 1641
终归单人心
终归单人心 2020-12-31 11:45
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

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 11:57

    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.

提交回复
热议问题