Server side variable within a JS file

Discussion in 'Javascript' started by Dan, Jan 5, 2007.

  1. Dan

    Dan Guest

    Excuse me if i'm being a bit thick here, but is it possible to
    reference a server side variable within an embedded js source file.

    For example, my test.js file contains

    alert('<%=tmpVar%>');

    and my aspx page contains:-

    <script type="text/javascript" src="test.js">

    This simple pops up an alert box containing <%=tmpVar%>, rather than
    the actual value of my server side variable.

    I can understand why it doesn't parse it to my test.js file.

    Anyone got any suggestions on how i can do this? I don't really want to
    have to include my javascript in my aspx header, due to maintenance
    issues.

    Thanks in advance for any suggestions.

    Dan
     
    Dan, Jan 5, 2007
    #1
    1. Advertising

  2. Dan wrote:

    > For example, my test.js file contains
    >
    > alert('<%=tmpVar%>');
    >
    > and my aspx page contains:-
    >
    > <script type="text/javascript" src="test.js">
    >
    > This simple pops up an alert box containing <%=tmpVar%>, rather than
    > the actual value of my server side variable.
    >
    > I can understand why it doesn't parse it to my test.js file.


    You would need to make sure your server preprocesses the file e.g.
    <script type="text/javascript" src="file.asp"></script>
    meaning you have asp on the server that dynamically generates
    client-side JavaScript code.
    A static .js file is just that, a static file, the server will simply
    pass it on and your ASP stuff does not get processed at all.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jan 5, 2007
    #2
    1. Advertising

  3. Dan

    Hal Rosser Guest

    "Dan" <> wrote in message
    news:...
    > Excuse me if i'm being a bit thick here, but is it possible to
    > reference a server side variable within an embedded js source file.
    >
    > For example, my test.js file contains
    >
    > alert('<%=tmpVar%>');
    >

    If you take the quotes away, it will probably work like you want.
    The tag <%=tmpVar%> returns a string, so it "is already" a string - putting
    the expression in quotes make the string "not asp" but a literal string.
    in other words, make a minor modification like so:
    alert(<%tmpVar%>); // **(leave out the quotes)**
     
    Hal Rosser, Jan 6, 2007
    #3
  4. Dan

    Hal Rosser Guest

    "Hal Rosser" <> wrote in message
    news:XaGnh.20992$...
    >
    > "Dan" <> wrote in message
    > news:...
    >> Excuse me if i'm being a bit thick here, but is it possible to
    >> reference a server side variable within an embedded js source file.
    >>
    >> For example, my test.js file contains
    >>
    >> alert('<%=tmpVar%>');
    >>

    > If you take the quotes away, it will probably work like you want.
    > The tag <%=tmpVar%> returns a string, so it "is already" a string -
    > putting the expression in quotes make the string "not asp" but a literal
    > string.
    > in other words, make a minor modification like so:
    > alert(<%tmpVar%>); // **(leave out the quotes)**
    >


    OOPS - I told ya wrong - didn't notice the code was in a "js" file.
    The code needs to be in your asp file to get processed by the server.
     
    Hal Rosser, Jan 6, 2007
    #4
  5. Dan

    King Albert Guest

    "Dan" <> wrote in
    news::

    > Excuse me if i'm being a bit thick here, but is it possible to
    > reference a server side variable within an embedded js source file.
    >
    > For example, my test.js file contains
    >
    > alert('<%=tmpVar%>');
    >
    > and my aspx page contains:-
    >
    > <script type="text/javascript" src="test.js">
    >
    > This simple pops up an alert box containing <%=tmpVar%>, rather than
    > the actual value of my server side variable.
    >
    > I can understand why it doesn't parse it to my test.js file.
    >
    > Anyone got any suggestions on how i can do this? I don't really want to
    > have to include my javascript in my aspx header, due to maintenance
    > issues.
    >
    > Thanks in advance for any suggestions.
    >
    > Dan
    >



    Have your serverside program write js code to initialize tmpVar
    'litteraly' on your page.

    My VB is a bit rusty, so the code below should be considered pseudocode:


    dim tmpVar
    tmpVar=666
    ....
    Response.Write "<html><head>"
    Response.Write "<script type='text/javascript'>"
    Response.Write "var tmpVar=" & tmpVar
    Response.Write "</script>
    Response.Write "<script type="text/javascript" src='test.js'/>"
    Response.Write "</head>"
    ....

    Here we've created a js global variable tmpVar you can now reference in
    your js file like so :

    alert(tmpVar);


    regards

    Ward
     
    King Albert, Jan 6, 2007
    #5
  6. Dan

    VK Guest

    Dan wrote:
    > Excuse me if i'm being a bit thick here, but is it possible to
    > reference a server side variable within an embedded js source file.
    >
    > For example, my test.js file contains
    >
    > alert('<%=tmpVar%>');
    >
    > and my aspx page contains:-
    >
    > <script type="text/javascript" src="test.js">
    >
    > This simple pops up an alert box containing <%=tmpVar%>, rather than
    > the actual value of my server side variable.
    >
    > I can understand why it doesn't parse it to my test.js file.
    >
    > Anyone got any suggestions on how i can do this? I don't really want to
    > have to include my javascript in my aspx header, due to maintenance
    > issues.
    >
    > Thanks in advance for any suggestions.


    You may use the fall-back segment of the <script> element for that. A
    <script> element with src attribute set consists of two blocks:
    1) the source file pointed by src
    2) fall-back code between <script> tags for UAs without src support.

    If src attribute is set is supported then anything between <script> tag
    is automatically ignored but still available at run-time as
    document.scripts.text

    Because for many years already there is not a single UA w/o script src
    support, the fall-back part left empty:
    <script src="source.js"></script>
    or used for some other purposes, say for copyright info:

    <script src="source.js">
    Copyright 2006 Acme, Inc.
    </script>

    Another use is exactly for your case: to provide session values for a
    static library:

    <script src="source.js">
    {data1:"value1", data2:"value2"}
    </script>

    so on ASP/PHP it would be:

    <script src="source.js">
    <processing instructions>
    </script>

    Of course JSON format is much more convenient here, then the run-time
    task as simple as:

    // presuming this is the first script element on the page:

    var params = eval(document.script[0].text);
     
    VK, Jan 7, 2007
    #6
  7. Dan

    Evertjan. Guest

    Dan wrote on 05 jan 2007 in comp.lang.javascript:

    > Excuse me if i'm being a bit thick here, but is it possible to
    > reference a server side variable within an embedded js source file.
    >
    > For example, my test.js file contains
    >
    > alert('<%=tmpVar%>');
    >
    > and my aspx page contains:-
    >
    > <script type="text/javascript" src="test.js">
    >
    > This simple pops up an alert box containing <%=tmpVar%>, rather than
    > the actual value of my server side variable.


    Use:

    <script type="text/javascript" src="testjs.asp">

    containing perhaps a session variable:

    alert('<%=session("tmpVar")%>')

    [classic asp solution, feel free to port it to the unknown (to me) asp.net]


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jan 7, 2007
    #7
  8. Dan

    bhaskarareddy

    Joined:
    Jun 11, 2012
    Messages:
    2
    We can pass the serverside value is as below syntax
    <%=variblevalue%>

    for more details refer this link

    Server side variable to js
     
    bhaskarareddy, Jun 11, 2012
    #8
    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:
    548
    Mythran
    Jan 22, 2005
  2. Thor
    Replies:
    1
    Views:
    1,045
    Mikkel Heisterberg
    Jul 2, 2003
  3. Replies:
    0
    Views:
    486
  4. Jack
    Replies:
    3
    Views:
    241
  5. JT
    Replies:
    5
    Views:
    158
    Ray Costanzo [MVP]
    Sep 10, 2004
Loading...

Share This Page