server side global variable failure

Discussion in 'Javascript' started by Bruce, Jan 20, 2006.

  1. Bruce

    Bruce Guest

    Can anyone tell me why this doesn't work?

    My end goal here is to be able to write Javascript library modules that
    can be used by both client and server side code. My test here consists
    of 2 files:

    testGlobal.asp :

    <%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
    <script language="JavaScript" type="text/javascript" runat="server"
    src="testGlobal.js"></script>
    <%
    Response.Write("Global value=" + testGlobal());
    %>

    testGlobal.js :

    var globalVal = "5";

    function testGlobal() {

    return globalVal;
    } /* end of testGlobal */

    My problem is that when I call testGlobal() from the server side,
    globalVal is undefined. If I move it inside the function, all is well.

    Why can't testGlobal() see the variable globalVal ?

    Thanks,
     
    Bruce, Jan 20, 2006
    #1
    1. Advertising

  2. Bruce wrote:


    > My problem is that when I call testGlobal() from the server side,
    > globalVal is undefined. If I move it inside the function, all is well.
    >
    > Why can't testGlobal() see the variable globalVal ?


    That is some unfortunate oddity in classic ASP, there might be an old
    article on msdn.microsoft.com that explains that better but mainly
    <script runat="server" src="file.js"></script>
    is only useful for declaring functions to be used then in <% %>.

    It is a pain for instance if you wanted to do prototype based
    inheritance as if you have e.g.
    function F () {}
    F.prototype.methodName = function () {}
    in a <script runat="server"> then later on you can use the function F in
    <% %> blocks as the function declaration is being processed but the
    assignment to the prototype is either not processed at all or I think
    only after your <% %> blocks meaning if you do
    <%
    var f = new F();
    you don't have the method available.




    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jan 20, 2006
    #2
    1. Advertising

  3. Bruce

    Bruce Guest

    > But it's not what you want because the ".js" file has <% and %>. Is this correct?

    Right. I can still call the function if I exclude "<%" and "%>", but
    for a reason I don't understand the function can't access the variable.

    If I use "<%" and "%>" then I can't use the .js file on the client side
    and I want to use it on both the client and server.
     
    Bruce, Jan 20, 2006
    #3
  4. Bruce

    McKirahan Guest

    "Bruce" <> wrote in message
    news:...
    > > But it's not what you want because the ".js" file has <% and %>. Is this

    correct?
    >
    > Right. I can still call the function if I exclude "<%" and "%>", but
    > for a reason I don't understand the function can't access the variable.
    >
    > If I use "<%" and "%>" then I can't use the .js file on the client side
    > and I want to use it on both the client and server.
    >


    The following works almost the way (I think) you want:

    [testGlobal.js follows]

    //<%
    // testGlobal.js
    var globalVal = "5";
    function testGlobal() {
    return globalVal;
    }
    //%>

    [testGlobal.asp follows]

    <%@ LANGUAGE="JAVASCRIPT" CODEPAGE="1252" %>
    <!--#include file="testGlobal.js"-->
    <% Response.Write("<br>Global value=" + testGlobal()); %>
    <html>
    <head>
    <script type="text/javascript" src="testGlobal.js"></script>
    </head>
    <body onload="alert('Global value=' + testGlobal())">
    </body>
    </html>


    It shows the execution of the function servier-side and client-side.

    However, the server-size execution outputs the unwanted "//".

    Perhaps it will give you some ideas... Good luck.
     
    McKirahan, Jan 20, 2006
    #4
  5. Bruce

    Bruce Guest

    Yes, it works exactly the way I want except for the display of "//" as
    you pointed out. Any ideas on how to get rid of that?
     
    Bruce, Jan 20, 2006
    #5
  6. Bruce

    McKirahan Guest

    "Bruce" <> wrote in message
    news:...
    > Yes, it works exactly the way I want except for the display of "//" as
    > you pointed out. Any ideas on how to get rid of that?
    >


    Change the background to black then you can't see it. :)

    Change it to something you want to see; for example,

    // <b>Hello World</b> //<%

    You might want to open a new post about just this.
     
    McKirahan, Jan 20, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mythran
    Replies:
    2
    Views:
    528
    Mythran
    Jan 22, 2005
  2. Ilias Lazaridis
    Replies:
    0
    Views:
    593
    Ilias Lazaridis
    Feb 1, 2005
  3. Replies:
    0
    Views:
    497
  4. Jack
    Replies:
    3
    Views:
    226
  5. -
    Replies:
    0
    Views:
    138
Loading...

Share This Page