I have a form that lets users select checks, and when submitted, creates a PDF, which opens in a new browser tab. It doesn’t have any branding, and will probably open in a plugin anyway, so I don’t want it taking over my site’s tab. So I set the form’s target to _blank.
But it’s possible for the user to submit the form without enough information to create the PDF, in which case I flag the error (server-side) and re-render the form. But because I set the form’s target, this re-render opens in a new tab as well, and that’s not what I want – in this case, I want it to behave as if target were _top.
So the question is: Can I change the browser’s rendering target server-side?
Yes, I know that this can be done with client-side JavaScript, but JS annoys me, and I have to do the validation server-side anyway. I may end up having to use it, but please don’t suggest it as an answer – I’m more curious if what I’m attempting can even be done.
PS: I’m on Ruby on Rails 2.3.8, in case anyone knows a framework-specific solution.
No. This is a purely client-specific feature. As a matter of fact, it’s quite possible to get a browser that supports only one window and where the
targetattribute would have simply no effect. There were even efforts to make this attribute disappear from future HTML standards completely (for instance, the XHTML branch had no such attribute).The only overlap that I can think of between HTML and HTTP are the
<meta http-equiv>tags (where HTML can affect otherwise HTTP-controlled behavior). HTTP is a transfer protocol, designed to work with about just any kind of data. Letting it control presentation would be a pretty terrible mix of concerns.Fortunately, we live in a JavaScript-enabled world. It is rather easy to validate a form using an AJAX request, especially with libraries like jQuery.
For instance, this script performs a POST request to an URL (in this case,
/pdf/validate) and expects the page to send back “ok” (if everything’s good) or something else if there was an error.