Welcome to WebmasterWorld Guest from 54.161.110.186

Forum Moderators: ocean10000

Message Too Old, No Replies

Irritating issue with simple if in classic asp

     
11:31 pm on Jan 27, 2010 (gmt 0)

5+ Year Member



I am trying to add a 'http://' for URLs on my website that are missing it. Otherwise the URLs go to 'http://www.example.com/www.example.net/' but I want them to be going to 'http://www.example.net/' if you understand what I mean.

I have added this bit of code to fix it:
<a href='<%if instr(WebAddress,"http://") = "" then response.Write("http://")%><%=(RsParent.Fields.Item("WebAddress").Value)%>' target="_blank"><%=(RsParent.Fields.Item("WebAddress").Value)%></a>

The code doesn't work properly though. For URLs with the 'http://' it adds an extra 'http://' rather than leaving it blank. For URLs missing the 'http://' I want to add one in and for URLs with the 'http://' I want to leave it blank. I've also tried an if-then-else-end if statement but the outcome is exactly the same.

Can anyone help?

[edited by: marcel at 1:24 pm (utc) on Jan. 28, 2010]
[edit reason] examplified [/edit]

12:00 am on Jan 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Off the top of my head and without verifying I would say

address= here whatever you do to get the address
address2 = left(address),7)

if address2 = "http://" then
'
else
response.write("http://") and the rest of your code

end if

left(address),7) checks the 7 first characters of the initial address and if it's not http:// it will execute else. Give it a try.

4:06 am on Jan 28, 2010 (gmt 0)

10+ Year Member



instr() returns a position where the substring was found. 0 (or -1?) if not found.

It does not return a string like you are using to compare it to.

7:41 am on Jan 28, 2010 (gmt 0)

5+ Year Member



Hi Staffa. Thanks for the info. I will play around with this.

bmcgee, I actually wrote the string as <a href='<%if instr(WebAddress,"http://") = 0 then' but found it wasn't working. Maybe I'll try it a different way. Thanks for your suggestions.

12:41 pm on Jan 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Something doesn't seem to be right:
<a href='<%if instr(WebAddress,"http://") = "" then response.Write("http://")%><%=(RsParent.Fields.Item("WebAddress").Value)%>' target="_blank"><%=(RsParent.Fields.Item("WebAddress").Value)%></a>

In the InStr check you are checking the variable 'WebAddress' but later in the code you are using 'RsParent.Fields.Item("WebAddress").Value'

Are you sure the WebAddress variable has been given a value?

I think the following will work:

<a href='<%if instr(RsParent.Fields.Item("WebAddress").Value,"http://") = 0 then response.Write("http://")%><%=(RsParent.Fields.Item("WebAddress").Value)%>' target="_blank"><%=(RsParent.Fields.Item("WebAddress").Value)%></a>
12:41 pm on Feb 2, 2010 (gmt 0)

5+ Year Member



no no no! instr outputs a number not a string!, and I think you your WebAddress variable has not been populated.


This should do a better job...(not tested)
<%
WebAddress = RsParent.Fields.Item("WebAddress").Value
if left(WebAddress,7)="http://" then WebAddress=mid(WebAddress,8)
response.write "<a href='http://"& WebAddress &"' target='_blank'>"& WebAddress &"</a>"
%>
4:47 pm on Feb 2, 2010 (gmt 0)

5+ Year Member



Oh right. Yes, that sounds like it could work. I'll give it a go.

THanks.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month