JavaScript post form data to new window without "target"
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 开发者_如何学Gomost 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 :)
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.
In Javascript, intercept the form submission, populate a second form with only hidden fields with the values you want, and submit that form instead.
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';
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..
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).
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.
精彩评论