views:

820

answers:

6

What? I would like to be able to do a post request (does not have to be form data) to a new window without using the target attribute (XHTML validation).

Why? I have a webapp (using jQuery) where the user selects a number of entries to print. Each entry id should be sent to a processing page that will display a printable version of the entires. I don't want to direct the user away from the webapp to print and I cannot use get in a popup because the number of entry ids might become too large to fit in a URL.

I have googled and read several forums and most people seem to suggest either the target attribute directly or inserting it using JavaScript. I was hoping to avoid both these solutions.

Is this even possible and if so, how?

Thanks in advance :)

A: 

To target the top of the current page and break out of any frameset currently in use you would use <a href="page.htm" target="_top"> in HTML. In Javascript you use:

top.location.href = 'page.htm';

To target the current page or frame you can use <a href="page.htm" target="_self"> in HTML. In Javascript you use:

self.location.href = 'page.htm';

To target the parent frame you can use <a href="page.htm" target="_parent"> in HTML. In Javascript you use:

parent.location.href = 'page.htm';

To target a specific frame within a frameset you can use <a href="page.htm" target="thatframe"> in HTML. In Javascript you use:

top.frames['thatframe'].location.href = 'page.htm';

To target a specific iframe within the current page you can use <a href="page.htm" target="thatframe"> in HTML. In Javascript you use:

self.frames['thatframe'].location.href = 'page.htm'; 
Luca Matteis
Thanks for the reply, but this is not what I asked for. My question has nothing to do with frames. I want to be able to do a post request to a new window (not frame).
Thomas
A: 

The attribute target of the <form> element is valid in XHTML 1.0 transitional..

If you are using XHTML 1.1 or 1.0 Strict then the <iframe> is not a valid tag in the first place..

Gaby
True, but then I am not using any iframe tags.
Thomas
ok just framesets then .. so .. just use target and xhtml transitional :)
Gaby
+2  A: 

This is what the target attribute is for. Set it to "_blank" to open a new window. If you want your markup to be valid (what's the point? I assure you that your users could care less about the validity of your markup!), then use Javascript to set the target on page load...

window.onload = function() {
  document.forms[0].target = '_blank';
}

There is no other way.

Josh Stodola
Ok, thanks. I was just hoping there would be a way. I think I'll go with ScottEs solution since I already have a pretty powerful AJAX backend.
Thomas
A: 

You could use Javascript window.open to "pop" the new window and use GET parameters in the URL to pass arguments... But IMO it's worse than using the target attribute in transitional doctype.

The best way to do it is a "fake" new window that is created on the fly in the DOM and populates itself by AJAX calls. This way you don't "break" the user experience (by opening a new window).

AlexV
I had thought of creating a popup directly in the DOM, but this causes problems when the user prints (will probably print the entire page). Thanks for the tip though.
Thomas
Keep in mind that you can add a "noprint" class to the whole popup and add a "print" CSS that will do a display : none for this class.
AlexV
A: 

In Javascript, intercept the form submission, populate a second form with only hidden fields with the values you want, and submit that form instead.

bart
This only moves the problem over to another form as far as I can see... unless this form is on another page. Since a popup can talk to its parent (as long as it is on the same domain) (I think) I could popup a new window with a hidden form, populate it and then submit that (in the new window). This would have the same effect.Not sure if this is what you had in mind, but it got me thinking so thanks alot :)
Thomas
A: 

If you set some hidden field in the page that opens the new window (call it entryIds) before you open a new window from the calling page, you can reference it in the opened page like so:

$(function() {
    var $opener = $(window.opener.document);
    var entryIds = $opener.find("#entryIds").val();
    // do something with the ids
});  

The disadvantage here is that you are now left to display your entry details via javascript as well. No need to do a form post at all.

If you're feeling frisky you could always set the entry ids in a session or cookie (via a web service call) on the opener page before you open the new window, and grab the session on the opened page so you can build up the print page on the server side instead.

ScottE
Thanks, hadn't thought of the session/cookie approach. Think I'll use that in combination with AJAX as you say. Thanks again :)
Thomas