Could you tell us a little more about what you're trying to do?
For example what is the user typing the url in to?
A text field in a form?
Also, why do you need to prepend "http://www." to the url?
Some urls may not work with the "www." prepended to them, others might use the https protocol.
What about if the user types in just the "www."?
At a first glance, this sounds like it might be better done server-side.
Edit: For example, if you are using PHP you can use parse_url()