• Post Reply Bookmark Topic Watch Topic
  • New Topic

Got a very strange JS problem here  RSS feed

 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys.

I've got a JavaScript setTimeout function on my admin.jsp page which when a user posts a news post it will refresh the page by sending them to the newspost page via admin.jsp?page=newspost. The code is below:



The news post form has got a GET var attached to the action EG action=admin.jsp?page=newspost&refresh=true

When I type some news and press Post the timeout will kick in but it will redirect to the index.jsp page and not the admin.jsp?page=newspost page as where it's meant to go. Why on gods earth is it not sending me to where ive specified the redirect to go in the JavaScript code??? Help much appreciated and many thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66260
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a JSP you should be using a server-relative URL that begins with the contact path. That eliminates all first of page-relative URL issues.
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:In a JSP you should be using a server-relative URL that begins with the contact path. That eliminates all first of page-relative URL issues.


My JSP's only contain JSTL EL tags. All my actual Java code are kept away in class files. My index page is actually a servlet which forwards certain attributes to my index.jsp page, then EL's in my JSP's take care of the forwarded request attributes. How could a JavaScript redirect affect this?
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just forget about it being a java webapp, why is the javascript code redirecting me to my index page when ive statically put in the URL???
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66260
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Portman wrote:How could a JavaScript redirect affect this?

How the redirect is affected is irrelevant. It's the URL that matters.

It's easy to use the EL to grab the context path to format a seder-relative URL. Because of the way Java web apps work, page-relative URLs are fragile and error-prone.

It's not possible to "forget" that the web app is Java-based -- it's fundamental to the web app, in the same way that a webpack-based JS app has its own patterns for how to use URLs.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66260
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Portman wrote:Just forget about it being a java webapp, why is the javascript code redirecting me to my index page when ive statically put in the URL???

You haven't. The URL you are using is page-relative and resolved against the current URL. It's a bad way to code regardless of the type of web app as how it resolved is dependent on outside factors -- in this case, the current URL. In a Java web app, server-relative URLs are the best way to avoid issues.
 
Michael Portman
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Michael Portman wrote:Just forget about it being a java webapp, why is the javascript code redirecting me to my index page when ive statically put in the URL???

You haven't. The URL you are using is page-relative and resolved against the current URL. It's a bad way to code regardless of the type of web app as how it resolved is dependent on outside factors -- in this case, the current URL. In a Java web app, server-relative URLs are the best way to avoid issues.


It doesnt matter now anyway cos im re-coding the site.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!