简体   繁体   中英

Concatenating href strings on javascript

I am trying to link to a website using info windows on google maps . I adapted the example as follows:

var contentString = '<div id="content">'+
      '<div id="siteNotice">'+
      '</div>'+
      '<h1 id="firstHeading" class="firstHeading">display info</h1>'+
      '<div id="bodyContent">'+
      'page number '+
      pageno +
      '<p>page , <a href="http://www.myaddress.com/" + String(pageno)>link</a> </p>'+
      '</div>'+
      '</div>';

The problem is that instead of landing to www.myaddress.com/pageno, the link always sends me to www.myaddress.com

How can I append the pageno correctly?

You need to cut the string, concatenate the pageno, and then pick up the string again:

var contentString = '<div id="content">'+
      '<div id="siteNotice">'+
      '</div>'+
      '<h1 id="firstHeading" class="firstHeading">display info</h1>'+
      '<div id="bodyContent">'+
      'page number '+
      pageno +
      '<p>page , <a href="http://www.myaddress.com/' + pageno + '">link</a> </p>'+
      '</div>'+
      '</div>';

The String -conversion is not needed either, since JavaScript does that internally, much like Java has a toString() method on it's classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM