?
Solved

Access unnamed form

Posted on 2007-08-09
7
Medium Priority
?
592 Views
Last Modified: 2013-11-05
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
Comment
Question by:L00M
  • 3
  • 2
  • 2
7 Comments
 
LVL 18

Expert Comment

by:Morcalavin
ID: 19662677
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
 
LVL 11

Expert Comment

by:raj3060
ID: 19662748
if you have only one form then:

document.forms[0].
0
 
LVL 11

Author Comment

by:L00M
ID: 19662770
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Author Comment

by:L00M
ID: 19662783
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
 
LVL 18

Accepted Solution

by:
Morcalavin earned 2000 total points
ID: 19662805
It should be:
myForms = document.getElementsByTagName("form");
myForms[1].onsubmit=hijacked;
0
 
LVL 11

Expert Comment

by:raj3060
ID: 19662820
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
 
LVL 11

Author Comment

by:L00M
ID: 19662821
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question