window.open vs form.submit

698 views Asked by At

what i need to do is, when the user clicks a certain div/image, to navigate to a different page. i use struts2 on backend.

so far, i have found 2 approaches, which produce the same effect (navigating to the other page):

  1. create an s:url tag with my action. add the div an onclick attribute, with the url as the parameter. in the javascript function, call window.open(url)

  2. create a form with the action. with jquery, call document.forms[].submit();

what i want to know is what is the difference between these two approaches. i am mostly interested in differences in terms of what happens inside struts, or in terms of dialog between browser and server, or what happens to the session

one difference (which i am not interested in) is that window.open also accepts parameters to open in a new window, and resize that window. i want to open the new action in the same window

a second difference (which is indeed more interesting) is that forms allow to use either POST or GET protocol. as for window.open, i am not sure which protocol is used

1

There are 1 answers

1
AudioBubble On

window.open opens a new window
window.location changes the location of the current page
forms[n].submit() submits the nth form to the server.

The window functions issue a GET request where you can add URL parameters if you need to send info to the server. There is a limitation to the number of characters you can send in a URL and it's usually not a good idea to send passwords in a GET request.

When you POST a form the parameters are in the message and not in the URL, thus they do not show up in the browser history nor are they affected by the url length limitation.

If your not sending data to the server for processing and you need to just navigate on an image click you should wrap it in an anchor tag

<a href="desired location"><img src=""/></a>