Results 1 to 8 of 8

Thread: PASSING ASP VARIABLES TO JAVA SCRIPT

  1. #1

    Thread Starter
    New Member
    Join Date
    May 1999
    Location
    Somerset, NJ
    Posts
    13
    I'm new to ASP and need some help.

    I've got an asp page that does some validation and then
    I pass a variable to a Client Side Java Script function.

    The JS function does not recognize the ASP variable.
    I tried using "document.all['<%=ASPVariableName%>'].value"
    with no success.

    The only way I found to pass anything to the JS function
    is to create a hidden field on the form and reference it
    like "form.HiddenFieldName.value"

    Any suggestions would be greatly appreciated.

  2. #2
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    Hi,
    Without seeing your code, it's hard to work out where you want the variable names to go.

    If you just want to put the ASP variable into a JS variable then try
    Code:
    MyVar = <% Request.QueryString("ASPvariable") %>
    or if you are using it to reference a field in a form:

    Code:
    document.all.form1.<%Request.QueryString("ASPvariable")%>.value
    Again, without your code I really don't know what you are trying to do but I hope this helps!

    Shaun
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  3. #3
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    oops...should be <% Response.Write Request.QueryString("ASPvariable") %>
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  4. #4
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Canada
    Posts
    264
    is there any place to see more of the code ?

    After the page is loaded, right click on the browser, and click the "view source" see if there is anything in the JavaScript that is wrong (like the variable value missing or something).

    try putting a

    response.write (request ("variable")) at the begining of the page, and see if you can see the value.

    The JavaScript / HTML don't even know it is an ASP page, so there is no diffrence if you use the ASP in JavaScript or in HTML, the ASP is being proccessed before the page is loaded (Server side).


    In the beginning the universe was created. This has made a lot of people very angry and is generally regarded as a bad idea.

    - Douglas Adams
    The Hitchhiker's Guide to the Galaxy

  5. #5
    Addicted Member
    Join Date
    May 2000
    Posts
    142
    yeah, if you need values from ASP, just define them outside your JS function, assign them values using simple ASP statement, and then pass them into your JS function.

    dvst8
    Secret to long life:
    Keep breathing as long as possible.

  6. #6
    Addicted Member
    Join Date
    May 2000
    Posts
    142
    asabi is absolutely right.

    the ASP is processed way before any client-side JS gets processed.

    that's why JS and ASP don't work togehter.

    dvst8
    Secret to long life:
    Keep breathing as long as possible.

  7. #7
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    Hi,
    We have solved the problem now! but thnx anyway. The way (and the only way I believe) to pass the ASP variable to the Javascript variable was top put this in the ASP page:
    Code:
    dim aBaseURL
    BaseURL="Http://VB-world.net"
    Response.Write("<Script>")
    Response.Write("var jBaseURL='" & aBaseURL & "'")
    Response.Write("</Script>")
    which then showed up on the HTML page as:
    Code:
    <Script>
    var jBaseURL='Http://VB-world.net'
    </Script>
    He could then access this Javascript variable as he would any other.

    There was more stuff to it as well but that is how I did that bit!!!

    Cheers,

    Shaun
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  8. #8

    Thread Starter
    New Member
    Join Date
    May 1999
    Location
    Somerset, NJ
    Posts
    13
    Thanks Shaun. It worked just fine.

Posting Permissions

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



Click Here to Expand Forum to Full Width