SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member Jacob.3336's Avatar
    Join Date
    Nov 2012
    Location
    California
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool Javascript text formatting

    How can I use javascript to add "http://www." to the beginning of a URL that the user types in?

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    Hi,

    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()

  3. #3
    SitePoint Member Jacob.3336's Avatar
    Join Date
    Nov 2012
    Location
    California
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Hi,

    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()
    I have a forum where the user needs to enter a URL and I want JavaScript to add "http://" if the user doesn't.

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by Jacob.3336 View Post
    I have a forum where the user needs to enter a URL and I want JavaScript to add "http://" if the user doesn't.
    OK, this'll do that:

    Code JavaScript:
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <title>Add 'http://' to text input</title>
      </head>
      <body>
        <form id="myForm">
          <p>
            <label for="url">Please enter a URL:</label>
            <input type="text" id="url" />
          </p>
     
          <input type="submit" value="Submit">
        </form>
     
      <script>
        var f = document.getElementById('myForm');
        f.onsubmit = function(){
          var url = f.url.value;
          if(!url.match(/^http/)) {
            url = 'http://' + url;
          }
          alert('You entered: ' + url);
        }
      </script>
      </body>
    </html>


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •