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?

2
  • 5
    You need to do something to teach your client that his all-javascript model is just wrong. Commented Dec 10, 2009 at 18:12
  • 1
    It's more like saying "Let's take a leaky boat" instead. Commented Dec 10, 2009 at 20:08

3 Answers 3

4

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I think you mean String.prototype.format so that you can use it as "a {1} test".format("quick");
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.
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.
0

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

e.g JavaScript equivalent to printf/string.format

Comments

0

I was looking for a similar thing and settled on Prototype's "Template" object.

From the Prototype examples

// the template (our formatting expression) var myTemplate = new Template( 'The TV show #{title} was created by #{author}.');

// our data to be formatted by the template var show = { title: 'The Simpsons', author: 'Matt Groening', network: 'FOX' };

// let's format our data myTemplate.evaluate(show); // -> "The TV show The Simpsons was created by Matt Groening."

Comments

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.