Link to home
Start Free TrialLog in
Avatar of shadie
shadie

asked on

One form, 2 buttons, each with different destinations.

Hi Experts,
I found this code online somewhere and need help modifying it. I want to specify the dimensions of the window that pops under the OnButton1 function. I've tried several different ways to get my window specs in there but none have worked.

Can anyone help?
Thanks!

(hopefully these points will be enough... it's all that I have left!)

---------------------------------------------
<script language=javascript>
<!--
function OnButton1()
{
      document.add.action = "preview.asp"
      document.add.target = "_blank";
      document.add.submit();
      return true;
}

function OnButton2()
{
      document.add.action = "<%=MM_editAction%>"
      document.add.target = "_self";
      document.add.submit();
      return true;
}
-->
</script>
<noscript>You need Javascript enabled for this to work</noscript>


<input name="Preview" type="submit" value="Preview" onclick="return OnButton1();">
<input type="submit" name="Submit" value="Submit" onclick="return OnButton2();">
</form>
Avatar of Roonaan
Roonaan
Flag of Netherlands image

You can try using this command inside preview.asp

<body onload="window.resizeTo(550,400);">

A form with target="_blank" does not allow for easy manipulation thru javascript. The window.open method which allows sizing however does not realy work easily with forms.

-r-
Avatar of shaggy_the_sheep
shaggy_the_sheep

Hi

Are you trying to use the button to create a complete new window?
if so you could use this....

function openWindow(width,height,url){
var top = (screen.Height/2)-(height/2);
var left = (screen.Width/2)-(width/2)

window.open(url,'','width=' + width + ',height=' + height + ',top=' + top + ',left=' + left,'');

}

Now on the buttons...

Change their type to button rather than submit and make their onclick event
onclick="openWindow(WIDTH,HEIGHT,'URL');

Note: Width and Height are not in single quotes, whereas Url is.

Richard
ASKER CERTIFIED SOLUTION
Avatar of Pravin Asar
Pravin Asar
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What do you want the function do?

Actually open a totally new window? With a specific Url?

Any more info would be useful

Richard
Avatar of shadie

ASKER

I have a web form where my Copy Writers will submit copy that will ultimately get uploaded to our site. Most of the writers are not up on their html, plus some of them will ultimately copy text from Quark Xpress. I am trying to make it 1. easy on them and 2. ensure that the copy conforms to the html standard in place at our site.

I am putting a "preview" link for the textarea where they enter their text. This way they can preview the copy for typos, etc. before submitting the form. The method I submitted with my question works, just that I can't seem to grab control of the widow that pops open. (they're a whiny bunch and I am trying to make sure that they see the preview window on top of the form window by making it smaller, say 500/300 pixels)
I have faced a similar situation,

I think you may find my code useful.

1. This opens a new window (centered on the screeen),
2. And sends a submitted form to the newly opened window.
3. Sets a focus() to new window.

Oh right

if the HTML they are uploading will be in a text area you need to do this....

function previewHtml(){
var width = 500;
var height = 300;

var top = (screen.Height/2)-(height/2);
var left = (screen.Width/2)-(width/2)

var newwindow = window.open('','','width=' + width + ',height=' + height + ',top=' + top + ',left=' + left,'');

newwindow.document.write (document.FORM.TEXTAREA.VALUE);
newwindow.focus()

}

now make your button
<input type="button" id="Preview" value="Preview" onclick="previewHtml()">

That should do what you want

Richard
Here is my post (look at the previous post), applied to your question..

<html>
<head>
<title>Form Submit Document</title>
</head>

<body>
<script language=javascript>
<!--
var sw;
function OpenCenWindow (url, winname, width, height) {
  var x = 0.5 *(window.screen.width - width);
  var y = 0.5 *(window.screen.height - height);
  var posStr = ", screenX=" + x + ", screenY=" + y;
  if (document.all) {
             posStr = ", left=" + x + ", top=" + y;
  }
  var pStr = 'resizable, status=no,width=' + width + ', height=' + height +
             ', alwaysRaised=1, toolbar=no, menubar=no, status=no';
  var sw = window.open (url, winname, pStr+posStr);
 
  return sw;
}

function OnButton1()
{
     document.add.action = "preview.asp";
     // Open a new window with specific dimension.
     var nw= OpenCenWindow ('','YourDest', 800, 600);
     document.add.target = "YourDest";
     document.add.submit();

     // Set focus to new window
     nw.focus();
     return true;
}

function OnButton2()
{
     document.add.action = "<%=MM_editAction%>"
     document.add.target = "_self";
     document.add.submit();
     return true;
}
-->
</script>
<noscript>You need Javascript enabled for this to work</noscript>
<form name="add">
<input name="Preview" type="submit" value="Preview" onclick="return OnButton1();">
<input type="submit" name="Submit" value="Submit" onclick="return OnButton2();">
</form>

</body>
</html>
Avatar of shadie

ASKER

pravinasar
It worked! Thank you. I took out the part where the window is forced to the center of the screen. Something about that jerky motion after the page loads that drives me nuts... but all-in-all, the code works great.

shaggy_the_sheep, I was working with your advice and simply changed that preview button to a button instead of submit. The window popped correctly but the textarea value was not passed to the pop window.

Thanks all for your help!
Thanks,

Use OpenCenWindow() from my last post.

This takes of a minor issue (when a sw is already created sw.moveTo () will fail..access
voilation), hence I used the posStr to center a window.

var sw;
function OpenCenWindow (url, winname, width, height) {
  var x = 0.5 *(window.screen.width - width);
  var y = 0.5 *(window.screen.height - height);
  var posStr = ", screenX=" + x + ", screenY=" + y;
  if (document.all) {
           posStr = ", left=" + x + ", top=" + y;
  }
  var pStr = 'resizable, status=no,width=' + width + ', height=' + height +
             ', alwaysRaised=1, toolbar=no, menubar=no, status=no';
  var sw = window.open (url, winname, pStr+posStr);
 
  return sw;
}
Avatar of shadie

ASKER

nice! I like that much better