Monday, November 5, 2007

ASP: Response.redirect (stupid function)

Hi all,

Most of ASP programmers should know that response.redirect() works fine and it is our primary option to redirect pages from one to another. But the sad story behind this function is that, it cannot be used after the server published the content to client side.

For example,
If i put
  1. response.write "Hi"
    [the content generated in server]

  2. response.write "Hi"
    response.redirect("somewhere.asp?act=action")
    [the content Hi is still generated by server and keep without sending to client side. Then why we write the content 'Hi' if the server do not publish it to client side? but redirects straight to somewhere.asp]

  3. response.write "Hi"
    response.flush
    response.redirect("somewhere.asp?act=action")
    [the content will be published to client side. And followed by error "The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content." That is right, the header already send to the client and how to redirect now?]

After googling, i found alternative function: server.transfer "somewhere.asp?act=action". But if you intent to use this function, you can't post the variables which i wanted to post "act=action".

Alright, here is the fix!

Keep aside the stupid(limited) asp script and get down to highly responsive Javascript. When ASP and Javascript is put together, the magic will happen.

This is not my own idea but the idea was contributed someone in forums when i googled around. So the credit all goes to him.

As we all know,
ASP: response.redirect("somewhere.asp?act=action")
is equal to
Javascript: window.location.href = "somewhere.asp?act=action";

Agree?

If you agree, you found the fix already :)

Now lets write down this in our asp page,
  1. response.write "Hi"
    response.flush
    response.write "<script>window.location.href = 'somewhere.asp?act=action';script>"

Tadaaaa!

ASP + Javascript = Magic

It is true, the magic happen for my case.

Thats all.

Oh ya, my next post will be about Maxis Broadband (Wireless) which said to be using 3.5G technology (HSDPA). For those who don't know the meaning of HSDPA, here is it, "High-Speed Downlink Packet Access". I will be testing the broadband today evening and implementing it to Aztech Wiress Router tomorrow at my father's office. Excited to surf using 3.5G for the first time.

Have a nice day!

No comments: