tags:

views:

59

answers:

1

Hi
With window.open method I open new site with parameters, which I have to pass by post method.I've found solution, but unfortunately it doesn't work. This is my code:

<script  type="text/javascript">    
function openWindowWithPost(url,name,keys,values)
{
var newWindow = window.open(url, name);
if (!newWindow) return false;
var html = "";
html += "<html><head></head><body><form id='formid' method='post' action='" + url +"'>";
if (keys && values && (keys.length == values.length))
for (var i=0; i < keys.length; i++)
html += "<input type='hidden' name='" + keys[i] + "' value='" + values[i] + "'/>";
html += "</form><script type='text/javascript'>document.getElementById(\"formid\").submit()</sc"+"ript></body></html>";
newWindow.document.write(html);
return newWindow;
}
</script>  

Next, I create arrays:

<script type="text/javascript">    
var values= new Array("value1", "value2", "value3") 
var keys= new Array("a","b","c") 
</script>  

And call function by:

<input id="Button1" type="button" value="Pass values" onclick="openWindowWithPost('test.asp','',keys,values)" />   

But, when I clik on this button, the site test.asp is empty (of course I try get pass values - Request.Form("b")).
How could I solve this problem, why I can't get pass values?

Thanks for help.

Regards

+1  A: 

Instead of writing a form into the new window (which is tricky to get correct, with encoding of values in the HTML code), just open an empty window and post a form to it.

Example:

<form id="TheForm" method="post" action="test.asp" target="TheWindow">
<input type="hidden" name="something" value="something" />
<input type="hidden" name="more" value="something" />
<input type="hidden" name="other" value="something" />
</form>

<script type="text/javascript">
window.open('', 'TheWindow');
document.getElementById('TheForm').submit();
</script>

Edit:

To set the values in the form dynamically, you can do like this:

function openWindowWithPost(something, additional, misc) {
  var f = document.getElementById('TheForm');
  f.something = something;
  f.more = additional;
  f.other = misc;
  window.open('', 'TheWindow');
  f.submit();
}

To post the form you call the function with the values, like openWindowWithPost('a','b','c');.

Note: I varied the parameter names in relation to the form names to show that they don't have to be the same. Usually you would keep them similar to each other to make it simpler to track the values.

Guffa
That's exactly what I was thinking. The original code posted did seem to work though...except on IE.
idealmachine
Thanks for reply. But I have a table, where in column are buttons. When I click this button, I open new page and pass some parameters. So I have to write function, and for each button call it and pass fixed arguments. My question, how could I rewrite Your above code to function?
luk4443
@luk4443: I added a function example above.
Guffa
Thanks, but I have still problem :( I'd like open new page test.asp, after click button. I've written Your code and I call function: <input id="Button1" type="button" value="Test" onclick="openWindowWithPost('a','b','c');" /> I try add adress test.asp to window.open('test.asp', 'TheWindow');but it still doesn't work.
luk4443
@luk4443: If you use the URL when you open the window, the page will just be replaced when you post the form. Put the URL in the `action` property of the form.
Guffa