homepage Welcome to WebmasterWorld Guest from 54.227.56.174
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
Forum Library, Charter, Moderators: ocean10000

Microsoft IIS Web Server and ASP.NET Forum

    
Irritating issue with simple if in classic asp
KRMwebdesign




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

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]

 

Staffa




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

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.

bmcgee




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

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.

KRMwebdesign




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

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.

marcel




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

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>

Seb7




msg:4072673
 12:41 pm on Feb 2, 2010 (gmt 0)

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>"
%>

KRMwebdesign




msg:4072835
 4:47 pm on Feb 2, 2010 (gmt 0)

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

THanks.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved