0

I have a javascript variable in a page default.aspx

var name="user1"

window.location="test.aspx"

this page will submit to test.aspx and in global.asax while firing Application_BeginRequest event ,i need to access the variable "name". i need to do this without cookies. Anyone can help me on this?

2
  • 1
    How will it submit? Where is that mysterious variable, in your java source? Commented Jul 8, 2012 at 20:03
  • OK, next question: Why do you need to submit a variable whoose value is already known serverside (to send it to the client in a script-tag)? Commented Jul 8, 2012 at 20:10

2 Answers 2

1

var name="user1" is a javascript variable that you could access inside Application_BeginRequest from the Request object assuming you have passed it to the test.aspx page when redirecting:

var name = "user1";
window.location.href = 'test.aspx?name=' + encodeURIComponent(name);

and then:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    string name = HttpContext.Current.Request["name"];
    if (!string.IsNullOrEmpty(name))
    {
        // the name variable was present in the request => do something with it
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

In this case use a <form> with method="POST". But bear in mind that if this is some sensitive information it should not even be in your javascript code. If you are doing some sort of authentication I would recommend you using Forms Authentication instead of reinventing some wheels here.
im using window.location for navigating to next page. How can i use POST here?
You can't. window.location.href uses GET. If you want to use POST you will have to use a <form> with method="POST" or an AJAX request. But as I said, if this is some sensitive information you shouldn't be doing anything like that at all because a malicious user could forge any request and replace the value with whatever he wants even if you are using POST. So don't be fooled into thinking that if the value is not in the query string you have it secured.
i need to use window.location in this scenario. then what is the use of creating a form here? anyway im not posting the page..please explain..
No, Global.asax runs on the server. This value needs to reach the server from the client. There are 2 ways to do this: GET or POST. Damn, I get the feeling that I am repeating myself and we are turning around in circles here :-)
|
1

If by "submit" you mean perform a POST or GET request then you'll need to pass name as a url-encoded string to the server as a form POST or as a querystring parameter in a GET request.

Then, in the Application_BeginRequest access the Request from the Current HttpContext

2 Comments

im using window.location to submit to the next page. But i dont want to show the querystring in URL
that is not the way you submit a POST request, changing window.location performs a "redirect" using GET.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.