javascript function returning a bool
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: javascript function returning a bool

  1. #1
    Join Date
    May 2002
    Posts
    1,435

    Resolved javascript function returning a bool

    My ASP page checks if it is running on localhost (i.e. my development machine) and prints a few debug messages that aren't seen when the page is hosted on an actual server.

    Coming from C++ as I do, I'm not sure if I am doing this right. Here is the function I am using:
    Code:
    <%
    function IsLocalhost()
    {
    	return 
    		Request.ServerVariables("HTTP_HOST")=="localhost" && 
    		Request.ServerVariables("SERVER_NAME")=="localhost";
    }
    %>
    If I print the two server variables I can confirm that they are "localhost" but the function always returns false when I use it like this:
    Code:
    <%
        if(IsLocalhost())
        {
          // Never executes code here
        }
        else
        {
          // Always executes code here
        }
    %>
    What am I doing wrong?

  2. #2

    Re: javascript function returning a bool

    First, are you using ASP or ASP.NET?

    Next, I'd add a couple of alert statements and actually verify what those values are, for example

    Code:
    alert(Request.ServerVariables("HTTP_HOST"));
    This should help you determine a bit more - you can also validate whether your function returns what you think it should.

  3. #3
    Join Date
    May 2002
    Posts
    1,435

    Re: javascript function returning a bool

    I am not using .NET, just ASP. You may have overlooked it in my post, but I am printing the variables and they both are "localhost"

    If I don't use a function ( IsLocalhost() ) the boolean check works:
    Code:
    	if(Request.ServerVariables("HTTP_HOST")=="localhost" && Request.ServerVariables("SERVER_NAME")=="localhost")
    	{
                   // This is what gets printed...
    		Response.Write("<p>localhost</p>");
    	}
    	else
    	{
    		Response.Write("<p>not localhost</p>");
    	}
    
    
    	if( IsLocalhost() )
    	{
    		Response.Write("<p>localhost</p>");
    	}
    	else
    	{
                   // This is what gets printed...
    		Response.Write("<p>not localhost</p>");
    	}

  4. #4
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,870

    Re: javascript function returning a bool

    The problem is that you are trying to get a boolean return from IsLocalhost().

    Code:
    <%
    	if(IsLocalhost())
    	{
    But the return is text, not boolean.

    Code:
    return 
    	Request.ServerVariables("HTTP_HOST")=="localhost" && 
    	Request.ServerVariables("SERVER_NAME")=="localhost";
    If you want that if statement to work, you are going to have to adjust the return from IsLocalhost().
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    May 2002
    Posts
    1,435

    Re: javascript function returning a bool

    But I don't understand why the return is text. Maybe it's because my backround is C++ but I assumed that the result of '==' is boolean and the result of '&&' is boolean so 'text==text && text==text' is boolean. Where am I going wrong? How do I change a text comparison to boolean?

  6. #6
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,870

    Re: javascript function returning a bool

    The return is text because you are just returning text type variables. If you want a boolean return, you have to use return true or return false.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7

    Re: javascript function returning a bool

    Sorry about missing the debug comments earlier - as for the actual issue, I don't think this is returning as text - the system appears to be interpreting it as a boolean for me. Writing this up on a jscript page as follows:

    Code:
    <%@ Language= JScript %>
    <%
    
    function IsLocalHost()
    {
       return Request.ServerVariables("HTTP_HOST") == "localhost" && Request.ServerVariables("SERVER_NAME") == "localhost";
    }
    
    if (Request.ServerVariables("HTTP_HOST") == "localhost" && Request.ServerVariables("SERVER_NAME") == "localhost")
    {
       Response.Write("<p>No function call - localhost</p>");
    }
    else
    {
       Response.Write("<p>No function call - not localhost</p>");
    }
    
    if (IsLocalHost())
    {
       Response.Write("<p>Function call - localhost</p>");
    }
    else
    {
       Response.Write("<p>Function call - not localhost</p>");
    }
    
    %>
    This returns the expected behavior of:

    Code:
    No function call - localhost
    
    Function call - localhost
    Does this look similar to your full code? What OS / IIS version are you running with when you're testing this? I tested this on Win2k3 w/ IIS 6, with ASP enabled (it's not by default in 2k3 and later.)

  8. #8
    Join Date
    May 2002
    Posts
    1,435

    Re: javascript function returning a bool

    Sorry to be so dense here, but I still don't understand why the return type is text.

    According to the javascript references that I am consulting, the result of a '==' comparison is a Boolean as is the result of '&&' so the result of this statement is a Boolean:

    Request.ServerVariables("HTTP_HOST")=="localhost"

    As is the result of this:

    Request.ServerVariables("SERVER_NAME")=="localhost"

    So now I have two Booleans and I do a logical AND which should also result in a Boolean - which is what I am returning.

    An example would really help.

  9. #9
    Join Date
    May 2002
    Posts
    1,435

    Re: javascript function returning a bool

    Thanks mmetzger, your sample code has shown me why mine is failing (at least I think it has). The only difference between your IsLocatHost() and mine is that your string comparisons were done all on one line whereas mine were split-up over three lines. That would work in C++ so I assumed it would in javascript. And since the code actually executes I can't understand what is really happening.

    Is my function just executing 'return' (with default value false) since it's the only thing on the first line?

    Is there no way to write the code as I have done in my original example so it is not all on one line?
    Last edited by 0xC0000005; September 26th, 2008 at 09:38 AM. Reason: spelling mistake

  10. #10

    Re: javascript function returning a bool

    Hmmm - putting the code on separate lines definitely causes the problem and it then returns "undefined" instead of either value.

    I can't find any reference to a line continuation character except when using string characters. After some fiddling, I found that the following worked:

    Code:
    function IsLocalHost()
    {
    	return "localhost" == "localhost" && 
    	   "localhost" == "localhost";
    }
    as long as there is something after the return statement (and yes, I checked by making the 2nd comparison unequal as well.) The odd thing is this seems to be very interpreter specific - it works on Safari and Firefox, but it does not work with ASP's JScript interpreter or on IE6.

    At the end of the day, the readability may suffer but I'd either use something with soft word wrap or just stick it all on one line.

  11. #11
    Join Date
    May 2002
    Posts
    1,435

    Re: javascript function returning a bool

    Thanks,

    I am going to stick with one line and make a note in the code.

    By the way, I just noticed that you are from Dallas so you may be interested in taking a look at the web site I am working on (link below). It's a site dedicated to the 1960s television show "Route 66" and several episodes were filmed in the Dallas area.

    Another fan of the show from Red Oak has been out recently taking photographs if filming locations in Crandall and Lewisville and they have recently been posted.

    This is also the site that has embedded Google Earth Street View maps that I was asking about last week.

    Ohio66 - Route 66 Filming Locations

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center