views:

127

answers:

2

So I have a client who does not allow any server side coding, except in rare occurences classic asp, so everything is HTML and javascript.

So basically I need to build a URL from the form and then redirect. Javascript isn't necessarily my thing, but this would take me 5 minutes in asp.net using String.Format.

Is there a String.Format method in javascript?

A: 

no, there's no such thing in javascript, but some people have already written a printf for js

e.g http://stackoverflow.com/questions/610406/javascript-printf-string-format

stereofrog
+2  A: 

Ouch, that sucks.

Stolen from another post:

String.format = function() {
  var s = arguments[0];
  for (var i = 0; i < arguments.length - 1; i++) {       
    var reg = new RegExp("\\{" + i + "\\}", "gm");             
    s = s.replace(reg, arguments[i + 1]);
  }

  return s;
}
jvenema
I think you mean String.prototype.format so that you can use it as "a {1} test".format("quick");
AutomatedTester
Obviously he didn't mean that as the string is quite clearly the first argument but in most situations prototyping would be a better option.
Andy E
True, you could use prototyping. The reasoning here is simply that c# (my language of choice) works in this manner. Personal preference though, prototyping would work fine too.
jvenema