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. Advertisements

  2. 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, Jan 5, 2007
    #2
    1. Advertisements

  3. Dan

    Hal Rosser Guest

    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

    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


    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

    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:
    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., Jan 7, 2007
    #7
  8. Dan

    bhaskarareddy

    Joined:
    Jun 11, 2012
    Messages:
    2
    Likes Received:
    0
    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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.
Loading...