Author |
Topic |
|
bjlt
Senior Member
1144 Posts |
Posted - 17 July 2001 : 10:19:01
|
I'm a newbie i wrote this code to test if the user agent is ie4+ or ns6+ or opera 4+, if so, i initial a var as 1 otherwise it's 0. the problem seems to be the variant. If i don't use it, everything's fine.
but it won't work with it. please take a look at the code, thanks.
<% Function UADetect() Dim intUA intUA=0 Set objBCap = Server.CreateObject("MSWC.BrowserType")
If (objBCap.Browser = "IE" And CInt(objBCap.Version) >= 4) _ or (objBCap.Browser = "Netscape" And CInt(objBCap.Version) >= 6) _ or (objBCap.Browser = "Opera" And CInt(objBCap.Version) >= 4) then intUA = 1 End If set objBCap = nothing End function %> <% UADetect %> <html> <head> </head>
<% if intUA=1 then %>intUA=1<% end if %>intUA=0
</html>
|
|
Doug G
Support Moderator
USA
6493 Posts |
Posted - 17 July 2001 : 10:41:47
|
quote: <% if intUA=1 then %>intUA=1<% end if %>intUA=0
This line looks a little funky. If you are trying to see the value of intUA, try something like:
<br>intUA Value is: <%=intUA%><br>
====== Doug G ====== |
|
|
bjlt
Senior Member
1144 Posts |
Posted - 17 July 2001 : 11:04:10
|
i just put it there for a test. it dosen't work, it won't show up even if in ie5. but if i put the if then directly after the detection, i.e. without using the variant, it's fine. strange, i just can't figure out whey.
i'll use it this way. UA=1 means it support css i use, if ua=0, the hardcoded width and height for 800x600 resolution will be used, as they don't work in css for these versions.
Edited by - bjlt on 17 July 2001 11:08:46 |
|
|
Dan Martin
Average Member
USA
528 Posts |
Posted - 17 July 2001 : 12:25:57
|
Any chance that code comes from a include file? <% UADetect %>
Just seeing if perhaps you are experiencing a problem I have experienced.
-Dan |
|
|
bjlt
Senior Member
1144 Posts |
Posted - 17 July 2001 : 12:31:54
|
quote:
Any chance that code comes from a include file? <% UADetect %>
Just seeing if perhaps you are experiencing a problem I have experienced.
-Dan
? I don't quite understand. I use the above code and save it as a test page. just doesn't work.
what do you mean by included file? a conflict? nope. the above code is all i have for test, not working. show every browser as intUA=0
|
|
|
Doug G
Support Moderator
USA
6493 Posts |
Posted - 17 July 2001 : 15:59:49
|
Well, on closer inspection there are some obvious problems.
You DIM a variable inside your function, which means it's scope is limited to the function. In other words, intUA disappears as soon as the function terminates. Functions are designed to return values to the calling code, so you need to add UADetect = intUA before the end of your function.
Then, when you call a function, you need to assign the return value somewhere, so your function call needs to be like: <% newVar = UADetect() %>
Finally you need to write out newVar (or whatever you name this new variable), not intUA which is long gone.
Your final results may resemble this:
<%Function UADetect() Dim intUA Dim objBCap '<<*** added this intUA=0 Set objBCap = Server.CreateObject("MSWC.BrowserType") If (objBCap.Browser = "IE" And CInt(objBCap.Version) >= 4) _ or (objBCap.Browser = "Netscape" And CInt(objBCap.Version) >= 6) _ or (objBCap.Browser = "Opera" And CInt(objBCap.Version) >= 4) then intUA = 1 End If set objBCap = nothing UADetect = intUA '<<***added this End function%>
<% Dim newVar '<<***added this newVar = UADetect() %>
<html> <body> The result of the brower test is: <% =newVar %>
</body> </html>
====== Doug G ======
Edited by - Doug G on 17 July 2001 16:01:04 |
|
|
bjlt
Senior Member
1144 Posts |
Posted - 17 July 2001 : 23:38:42
|
Thanks a lot.
I'm not very clear on this. I have to name => UADetect = intUA '<<***added this "UADetect", then it's the result of the fuction, when newVar=UADetect() I will get the result, and this is the way a function works, right?
I had the same problem last time add some rs. I set rs=nothing, then got nothing, just found I need to Dim a new variable for it before set it nothing. [:q]
btw, I noticed that on this forum, when you do copy/past, some code lose return while others do not, and some seems to preserve the layout well (indent, carrage etc.). what's the right way to post code here?
btw 2, I saw a lot of code does not close or clear rs or obj. Should I always close/clear them?
Thanks again, now my site can display as the user screen resolution for ie4+ ns6+ and opera4+ users, (not sure about ie4 and oera4, as I don't have them now) while others get 800x600.
|
|
|
Doug G
Support Moderator
USA
6493 Posts |
Posted - 17 July 2001 : 23:55:25
|
quote: I have to name => UADetect = intUA '<<***added this "UADetect", then it's the result of the fuction, when newVar=UADetect() I will get the result, and this is the way a function works, right?
Yes. To clear (or muddy) things a bit more, in VBScript, procedures are either Subs or Functions. A Sub procedure is a callable section of code that does not return a value. A Function procedure is a callable section of code that returns a value. You as the programmer determine the return value of a Function procedure by assigning a value to a variable with the same name as the Function procedure itself.
I have the same problem with cut & paste from this forum. I copy something and paste into Notepad, oops, open Wordpad and paste there OK. I don't think the forums store the Windows standard cr-lf for each line.
As far as closing objects, here's my rule: If I create an object with my code, I close it if applicable and set it to nothing in my code. I follow the "open late, close early" rule, which means I create the object only when the code needs it and close it as soon as the code is done with it.
====== Doug G ====== |
|
|
|
Topic |
|
|
|