• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 605
  • Last Modified:

Access unnamed form

I'm trying to add some form vaidation to a page where I have very little control over the original content of the page. The content comes from another application, and I'm unable to modify the code in that application. (Well, I could, but modifying that code makes for huge headaches at upgrade time....anyway....) Let's assume I can't modify the existing page code. I can, however, add code before the existing FORM. So, I plan on hijacking the onsubmit of that form like this:

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
  window.onload=hijackform;
  function hijackform() {
    document.test.onsubmit=hijacked;
  }
  function hijacked() {
    alert('hijacked');
    return false;
  }
</script>

That works great, if the form name is 'test'. However, in this case, the form doesn't have a name. The existing code is:

<FORM ACTION="/pls/test/bwckcoms.P_Regs" METHOD="POST" onSubmit="return checkSubmit()">

I guess I'll have to add that checkSubmit back into my code...

So, how do I reference that form? It's the second of two forms on the page.

TIA!
0
L00M
Asked:
L00M
  • 3
  • 2
  • 2
1 Solution
 
MorcalavinCommented:
you could do something like:
myForms = document.getElementsByTagName('form');

myForms would be an array containing all your form elements.  If it's the second form then it should be myForms[1].
0
 
raj3060Commented:
if you have only one form then:

document.forms[0].
0
 
L00MAuthor Commented:
I receive the error:
Microsoft JScript runtime error: 'document.myForms.1' is null or not an object

My current code is:

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
  window.onload=hijackform;
  function hijackform() {
    myForms = document.getElementsByTagName("form");
    document.myForms[1].onsubmit=hijacked;
  }
  function hijacked() {
    alert('hijacked');
    return false;
  }
</script>

The error occurs on the line:
    document.myForms[1].onsubmit=hijacked;
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
L00MAuthor Commented:
Ah, well, I'm testing it on a page with only one form. So I changed the 1 to a 0, but it still gives the same error for 0.

Here is my complete test code:


<HTML>
<HEAD>
  <TITLE>JS Sandbox</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
  window.onload=hijackform;
  function hijackform() {
    myForms = document.getElementsByTagName("form");
    document.myForms[0].onsubmit=hijacked;
  }
  function hijacked() {
    alert('hijacked');
    return false;
  }
</script>
<form method="post" action="showsubmit.asp">
  First Name:<br>
  <input type="text" name="fname" id="fname" value=""><br>
  Last Name:<br>
  <input type="text" name="lname" id="lname" value=""><br>
  <input type="submit" name="submit" id="submit" value="Submit">
</form>
</BODY>
</HTML>

0
 
MorcalavinCommented:
It should be:
myForms = document.getElementsByTagName("form");
myForms[1].onsubmit=hijacked;
0
 
raj3060Commented:
try this:

<HTML>
<HEAD>
  <TITLE>JS Sandbox</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
  window.onload=hijackform;
  function hijackform() {
    document.forms[0].onsubmit=hijacked;
  }
  function hijacked() {
    alert('hijacked');
    return false;
  }
</script>
<form method="post" action="showsubmit.asp">
  First Name:<br>
  <input type="text" name="fname" id="fname" value=""><br>
  Last Name:<br>
  <input type="text" name="lname" id="lname" value=""><br>
  <input type="submit" name="submit" id="submit" value="Submit">
</form>
</BODY>
</HTML>
0
 
L00MAuthor Commented:
You da man!

Final code for future solution seekers:

<HTML>
<HEAD>
  <TITLE>JS Sandbox</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
  window.onload=hijackform;
  function hijackform() {
    myForms = document.getElementsByTagName("form");
    myForms[0].onsubmit=hijacked;
  }
  function hijacked() {
    alert('hijacked');
    return false;
  }
</script>
<form method="post" action="showsubmit.asp">
  First Name:<br>
  <input type="text" name="fname" id="fname" value=""><br>
  Last Name:<br>
  <input type="text" name="lname" id="lname" value=""><br>
  <input type="submit" name="submit" id="submit" value="Submit">
</form>
</BODY>
</HTML>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now