javascript - How to split a function variable and use it later on? -


i'm working on hash system website i'm making, although of pages want hash require php id @ end. don't want have .php in hash url i'm trying split can insert later on.

an example of on html side.

<a href="#!/_radio/profile?id=3">link</a> 

this have far. works except variable isn't included @ end.

var radio = {};     radio = {  _jsinit: function () {      $(window).bind('hashchange', function () {         radio._currentpage();         radio.loadpage(radio._pagename, radio._extra)     });     $(window).trigger("hashchange")  },  _currentpage: function () {     this._pagename = location.hash.replace('#!/', '');     if (this._pagename == "") window.location = "#!/_radio/home";     this.values = this._pagename.split("?");     this._extra = this.values[1]; },  loadpage: function (page, extra) {     $('#content').fadeout(200).load('_files/_v2/_pages/' + page + '.php?' + + '', function () {       $('#content').fadein(400)     }); },  };  $(document).ready(function () {    radio._jsinit(); }); 

for before , after:

before: "#!/_radio/profile?id=2" after: "_files/_v2/_pages/profile.php?id=2" 

you can use regex find parts of string want:

function convertstr(str) {      var matches = str.match(/(\/[^\/]+)(\?.*$)/);      return "_files/_v2/_pages" + matches[1] + ".php" + matches[2];  }    // call function , display result in snippet  var result = convertstr("#!/_radio/profile?id=2");      document.write(result);

or, simpler version uses .replace():

function convertstr(str) {      return str.replace(/(^.*)(\/[^\/]+)(\?.*$)/, "_files/_v2/_pages$2.php$3");  }    // call function , display result in snippet  var result = convertstr("#!/_radio/profile?id=2");      document.write(result);


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -