Help with text link “onclick="javascript:func()”

Discussion in 'JavaScript' started by FastVCI, Nov 3, 2012.

  1. #1
    Hi,
    I'm very new to JavaScript.
    I need to setup a preview text link - users will be able to preview the entries before submitting the form.
    I managed to make it work using a form button but the “preview” link doesn't work.
    What I'm doing wrong?
    Your help will be appreciated.
    Thanks.

    <HTML>
    <HEAD>
    <TITLE>preview form</TITLE>
    <script language="JavaScript" type="text/javascript">
    function setFormAttrib(oForm, action, target){
    oForm.target = target;
    oForm.action = action;
    if (target=="_popup"){
    oForm.onsubmit = function(){
    return checkCheckBox(this) && createPopup(target);
    }
    }
    else{
    oForm.onsubmit = function(){
    return checkCheckBox(this);
    }
    }
    }

    function createPopup(target){
    var win = window.open("", target, "width=300,height=300,toolbar=0,menubar=0,location=0,status=0,scrollbars=0,resizable=1,left=20,top=20");
    w.focus();
    return true;
    }
    </script>
    </HEAD>
    <BODY>
    <FORM NAME="form" method="post" action="submit.aspx">
    <INPUT TYPE="TEXT" NAME="fname">Enter your name here<br>
    <INPUT TYPE="TEXT" NAME="email">Enter your email here<br>
    <BR>
    <P>
    <input type="submit" name="btnPreview" value="Preview Working" onClick="setFormAttrib(this.form, 'preview.aspx', '_popup');" />
    </P>
    <p>
    <p>
    <A HREF="javascript:void(0)" onClick="setFormAttrib(this.form, 'preview.aspx', '_popup');">Preview in a new window</A>
    </p>
    <p><input name="" type="submit"></p>
    </FORM>
    </BODY>
    </HTML>
    ---
    preview page
    ---
    <head>
    </head>
    <body>
    <body>
    <p>First Name: <% response.write(request.form("fname")) %>
    <br />
    Email: <% response.write(request.form("email")) %></p>
    </body>
    </body>
    </html>
     
    FastVCI, Nov 3, 2012 IP
  2. Slincon

    Slincon Well-Known Member

    Messages:
    1,319
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    180
    #2
    It's working fine for me - you might have a popup blocker setup in your browser that's preventing it from launching the popup window. Also, if the problem is in the asp script not showing the information when you click the preview button, it's because you're not submitting the form values to it when you click preview.
     
    Slincon, Nov 3, 2012 IP
  3. FastVCI

    FastVCI Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Slincon, thanks for your feed-back.
    On the test server the "Preview Working" button works - however it doesn't open a pop-up window as specified: width=300,height=300,toolbar=0,menubar=0,location=0,status=0,scrollbars=0,resizable=1,left=20,top=20

    The “Preview in a new window” link doesn't work.
    <A HREF="javascript:void(0)" onClick="setFormAttrib(this.form, 'preview.aspx', '_popup');">Preview in a new window</A>
    There is anything I should change?
    Thanks.
     
    FastVCI, Nov 3, 2012 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    an anchor doesn't have a property '.form' so 'this.form' should be outputting an error! As a rule accessing an element by passing 'this' in that manner is unreliable and shouldn't be used -- just as using the name attribute in scripts is NOT the recommended way of handling things anymore.

    Which is why your form should have an id on it, and your method should pass that ID to use 'document.getElementById' to target the form instead... and not waste time putting NAME on a element (form) that shouldn't have one anytime after nyetscape 4 was kicked to the curb a decade ago.

    Though it would be nice to see a proper form that had say... a fieldset, actual labels, didn't put a non-paragraph elements in paragraph tags, and in general used markup that wasn't HTML 3.2 style circa 1997. Just saying...

    You might want a modern doctype on there too since if you test in IE (which you should do from the start, test in as many browsers as possible) many things in scripting behave differently.

    Also hope you're writing this with a scripting off fallback -- given the popularity of script blocking these days.
     
    deathshadow, Nov 4, 2012 IP