Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't get "<%=Request.ServerVariables()%>" to work

Status
Not open for further replies.

ttobin

IS-IT--Management
Nov 22, 2010
3
US
I'm trying to do something that is (probably) VERY easy, but I sure can't figure it out! I want to create a user link on an aspx webpage for a user to click, and when they click it, depending on what their local address is, I want to open either of two webpages.

I believe I probably need to use a <%=Request.ServerVariables("LOCAL_ADDR")%> call, but I have tried 100 different ways of doing this, without any success. In fact, I can't get the Request.ServerVariables() call to return ANYTHING (no errors, but no data either).

In my (simple!) mind, I would like to do something from javascript like so:

<script language="javascript" type="text/javascript">
function AorB(urlA, urlB) {
var myip = <%=Request.ServerVariables("LOCAL_ADDR")%>;

if (myip == "192.10.4.7")
MyOpenWindow = window.open(urlA, '_blank');
else
MyOpenWindow = window.open(urlB, '_blank');
history.go(0);
}
</script>

Can anyone help with this??

Thanks!

-tom

P.S. if I have posted to the wrong forum I apologize, but I figured since I can't even figure out how to use ASP Server Variables that this was the correct place to start.
 
>var myip = <%=Request.ServerVariables(&amp;quot;LOCAL_ADDR&amp;quot;)%>;

If anything plausibly to work, it should be at least this.
[tt] var myip = [highlight]"[/highlight]<%=Request.ServerVariables("LOCAL_ADDR")%>[highlight]"[/highlight];[/tt]
(Client-side highlighted quote can be single-quote as well, I put quote to make you perplexe for a short while in order you pause there to think about it.)
 
javascript has absolutely no access to the server variables.


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
 
But it is let known. It does not access in real time.
 
Tsuji: I have already tried surrounding the <% %> call with single quotes (two-sets of double quotes will not create a valid script, as I'm sure you are aware). I also tried double-quotes on the outside, and single-quotes around 'LOCAL_ADDR'. I both cases, myip is set to the literal string value between quotes.

Chris: isn't there a way to execute javascript server-side instead of client-side, and in doing so is it not possible to access server-side variables that way?? In any case, I merely suggested using javascript; I can definitely use another method if one is available.

Thanks.

-tom
 
> I both cases, myip is set to the literal string value between quotes
Is it not what an ip address is? a string? (And, without the outer quotes, what would it be?!)
 
isn't there a way to execute javascript server-side instead of client-side,
Sort of, Jscript is the other language that ASP supports.

However it won't run server side as you have it written.

it has to be in ASP delimiters <% %> and the language declaration

<%@ language="javascript"%>

needs to be the first line of the page to tell the ASP interpreter to use jscript instead of vbscript, which is the default language.




Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
 
Tsuji: Without the outer quotes, the script simply fails (I don't get any error message, but neither does it successfully set the value of myip; If I place a line

alert("myip is " + myip);

immediately after the "myip = " line, it never executes.

On the other hand, if I use the outer quotes, then myip gets set to the LITERAL string between the quotes (ie "<%=Request.ServerVariables("LOCAL_ADDR")%>"). In other words, the expression does not get translated into an IP address, but it simply gets treated like a string, like "grass is green" or "the sun is hot". Get it?

Chris: Thanks for the suggestion. I can play some more trying to get it to execute server-side, as you suggest. Do you happen to know what would happen if I added at the end of my <script ....> statement the following:

runat="server"

Should that do anything for me??

Thanks again.

-tom
 
Sure, I know what it would be. My question is to let you know without external quote it simply means... non-sense, as what you showed in the original post.
 
runat="server" is a .NET framework instruction NOT "classic" ASP

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top