views:

130

answers:

6

I have an input field that saves a URL, I'd like this saved input to recognize when "Http//" is absent from the start of the variable but have no idea where to begin... is it possible to check only a portion of a string? - then have a function that will append if necessary?

+2  A: 

Something like this (writing by memory)?

if (url.toUpper(url.substring(0, 7) != "HTTP://")
  url = "http://" + url;
Eugene Mayevski 'EldoS Corp
`url.substring(0, 7).toUpperCase()`
Ferdinand Beyer
+5  A: 

A simple solution for what you want is the following:

var prefix = 'http://';
if (s.substr(0, prefix.length) !== prefix)
{
    s = prefix + s;
}

However there are a few things you should be aware of...

The test here is case-sensitive. This means that if the string is initially Http://example.com this will change it to http://Http://example.com which is probably not what you want. You probably should also not modify any string starting with foo:// otherwise you could end up with something like http://https://example.com.

On the other hand if you receive an input such as example.com?redirect=http://othersite.com then you probably do want to prepend http:// so just searching for :// might not be good enough for a general solution.

Alternative approaches

  • Using a regular expression:

    if (!s.match(/^[a-zA-Z]+:\/\//))
    {
        s = 'http://' + s;
    }
    
  • Using a URI parsing library such as JS-URI.

    if (new URI(s).scheme === null)
    {
        s = 'http://' + s;
    }
    

Related questions

Mark Byers
This is recomended.
Marwelln
A: 

You can use "StartsWith" a member of System.String.

if (url.ToUpper().StartsWith("HTTP://"))
{ 

}
Daniel James Bryars
This is not C#.
quantumSoup
if (isNotC#) { return "StartWith does not exist."; }
Hello71
The beauty of Javascript is that this can be done.
Casey Hope
http://msdn.microsoft.com/en-us/library/ms131452(v=VS.90).aspx
Daniel James Bryars
And, I just knocked up a console app, targeting .net 3.5 and this compiles and builds fine: string url = @"http://msdn.microsoft.com/en-us/library/ms131452(v=VS.90).aspx"; if (url.ToUpper().StartsWith("HTTP://")) { Console.WriteLine("Is this c#, the method is defined in Assembly mscorlib.dll, v2.0.50727."); }
Daniel James Bryars
@Daniel James Bryars: It seems that "this is not C#" refers to the question, whose tags currently (rev.1) read "javascript html variables" - no C#.
Piskvor
Opps I see, sorry.
Daniel James Bryars
A: 

Lifted from the Linkenizer (Null won't mind)

link = (link.indexOf('://') == -1) ? 'http://' + link : link;

This will prepend 'http://' to link if it can't find the :// indicating protocol. This won't work well if :// occurs elsewhere in the string, but it's good enough.

Examples:

http://www.google.com -> http://www.google.com
ftp://google.com      -> ftp://google.com
www.google.com        -> http://www.google.com

Since you said you are saving this URL, it would be a better idea to do this on the server-side, so clients who have js disabled won't mess up the links.

quantumSoup
Bear in mind that this will fail for `mailto:` links.
Ferdinand Beyer
+2  A: 

If you also want to allow "https://", I would use a regular expression like this:

if (!/^https?:\/\//i.test(url)) {
    url = 'http://' + url;
}

If you're not familiar with regular expressions, here's what each part means.

  • ^ - Only match at the beginning of the string
  • http - Match the literal string "http"
  • s? - Optionally match an "s"
  • : - Match a colon
  • \/\/ - Escape the "/" characters since they mark the beginning/end of the regular expression
  • The "i" after the regular expression makes it case-insensitive so it will match "HTTP://", etc.
Matthew Crumley
+1  A: 
if (url.indexOf('http://') != 0)
    url = 'http://' + url;
Casey Hope