Ramesh Rajendran
Ramesh Rajendran

Reputation: 38663

Anchor Tag Not working

I Have a two anchor tag for look like below

<a href="www.exx.com" target="_blank">
            AnnualBudget</a>

When i click the above Anchor tag ,Its not gone correct URL(For it's gone to Mydomainname/www.exx.com). But the same time below anchor tag is working and go to correct url .

<a href="https://www.exx.com" target="_blank">
            AnnualBudget</a>

Why www is not worked but https is worked ? And How can i solve this issue ?

Update :

Upvotes: 1

Views: 1114

Answers (1)

Uwe Keim
Uwe Keim

Reputation: 40736

Try putting a "http://" in front.

I.e.

<a href="http://www.exx.com" target="_blank">AnnualBudget</a>

"www" is not a protocol/scheme. HTTPS or HTTP are protocols.

Absolute URLs have to have a "scheme" in front, see details about URLs on Wikipedia.

Alternatively, this would also work:

<a href="//www.exx.com" target="_blank">AnnualBudget</a>

Update 1:

Since you comment that your input comes from the user, let me add this one:

enter image description here

(Although this refers to SQL injection, the same would be true for all user input).

Update 2:

To check the input for an absolute URL, do something like:

// Read from user input, e.g. (WebForms syntax!):
string my = MyTextBox.Text.Trim();

// Do some checking (this has be done much more thoroughly in real-life!)
if ( !my.StartsWith("http://") && !my.StartsWith("https://") )
{
    my = "http://" + my;
}

// Do something with "my", e.g. (again, WebForms syntax only):
MyHyperLink.NavigateUrl = my;

(Please note that I'm no MVC expert, the above pseudo-code uses WebForms syntax instead)

Upvotes: 4

Related Questions