-8

I know how to redirect from a jsp page using Javascript. But How can I forward using Javascript. Any function available ?

I need to work it like

<jsp:forward page="page_path"/> 

but using javascript. I can not use window.location.href or window.location.replace as both of these actually redirect, not forward.

Thanks

  • 2
    -1, you haven't at least once tried to google it, [e.g.](http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript) – linski Dec 11 '13 at 10:31
  • I do not know why people giving me negative ranking for this question without giving me right answer !! everybody giving answer of redirecting not forwarding. – Kayesh Parvez Dec 11 '13 at 10:43
  • 1
    Because you can not do that from javascript, see my answer. I have removed my downvote since the question is legit, though it did not seem so. I advise you to read the [FAQ - How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before you ask your next question :) – linski Dec 11 '13 at 11:04
  • Thanks. I have seen your answer. I wanted not redirect as forwarding is lighter than redirection. But I find it is troublesome and not possible actually. – Kayesh Parvez Dec 11 '13 at 11:10
  • You are welcome, yes, that is not possible. Maybe [AJAX](http://en.wikipedia.org/wiki/Ajax_(programming)) will suit your needs... – linski Dec 11 '13 at 11:14

3 Answers3

1

Java practices site sums it up nicely:

Forward

  • a forward is performed internally by the servlet
  • the browser is completely unaware that it has taken place, so its original URL remains intact
  • any browser reload of the resulting page will simple repeat the original request, with the original URL

Redirect

  • a redirect is a two step process, where the web application instructs the browser to fetch a second URL, which differs from the original
  • a browser reload of the second URL will not repeat the original request, but will rather fetch the second URL
  • redirect is marginally slower than a forward, since it requires two browser requests, not one
  • objects placed in the original request scope are not available to the second request

Forward happens while processing on the server,so you can not invoke it with JS, since JS execution is happening while processing client-side.

The only option I see is to redirect to the same URL, but append a query string parameter to instruct the server to perform a forward this time. However, that will reload the page.

Community
  • 1
  • 1
linski
  • 5,046
  • 3
  • 22
  • 35
0

have you tried-

window.history.forward()

it will work if there is next URL in the history list.

A.T.
  • 24,694
  • 8
  • 47
  • 65
-1

Just call:

window.location.href = "http://example.com";
BaBL86
  • 2,602
  • 1
  • 14
  • 13