Hi Friends...
I developed web application which contains script validations in most of the html pages in these validations i used document.forms[0] its working fine in IE , but my problem now i need to execute this application in FireFox browser,but FireFox is not supporting document.forms[0], so please tell me how to make generalised to handle document.forms[0] in all browsers.

Cheers
---------
Siva kumar

Things can be handled in both ways, by implementing some lines along with the documen.forms[index] itself, enhancing its support for the other modes of browsers:

var form = (( document.getElementById ) ? document.getElementById( "yourFormId" ) : document.forms["yourFormId"] );

// more stuff >>>

that should work in both browsers.

Things can be handled in both ways, by implementing some lines along with the documen.forms[index] itself, enhancing its support for the other modes of browsers:

var form = (( document.getElementById ) ? document.getElementById( "yourFormId" ) : document.forms["yourFormId"] );

that should work in both browsers.

Hi essential,
Thanks for your reply, it is difficult to change in all pages there are hundreds of pages,so please tell me how to handle when document.forms[0] is not worked i.e., i need to write generalised script if document.forms[0] not works

That's wierd, i keep on updating the lines, but it doesnt seems to update any changes on my post.

So i'll re-post it again:

var form = (( document.getElementById ) ? document.getElementById( "yourFormId" ) : document.forms["yourFormId"] )

it keeps on ignoring the bb tag.

Hi,

you can create a single .js and provide all the exceptions you need for the form processing using the document.getElementById procedure.

Make it on top of list right before any of the <script type="text/javascript" src="external.js"></script> tags in your page, so that you will able to filter it before it can reach any of those functions, that uses document.form[0] methods'.

Hi essential,
Can you please provide me example
Cheers
-------
Siva

Ok, i'l work on that one. I'l be back later...

Thank you essential
please don't forgot

Hi,

here is a simple demo, showing how you can handle your forms over the implemented lines in your 100's pages.

I've have a mis-leading tips on my first-post, so just disregard whatever i have in there.

Here's the content of the ff.js: ( keep this in a .js file, so that you don't have to rewrite all of its function on every page ).

var firefox = function() {
var form = document.getElementsByTagName("form");

   somefunc = ( function() { // apply new implementation under this mode.
   alert( "\nThe ( somefunc-FUNCTION ) has now been converted to handle forms in firefox mode" );
   alert( "\nYour form id: ( " + form[ 0 ].id + " ) -" ); // Lets output the form id >>>

// More stuff to follow >>>
   return false;
   } );
     
}; if ( document.getElementById ) {
  alert( "\n- Firefox mode is now activated." );
   window.onload = firefox; }

Assuming that this document is one of those 100's pages:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/css" href="#css_level21" media="all"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<title>www.daniweb.com :: DHTML / JavaScript / AJAX</title>
<style id="css21" type="text/css" media="all">
/* <![CDATA[ */

/* ]]> */
</style>

<script type="text/javascript">
// <![CDATA[
var somefunc = function() {
   var form = document.forms;
   alert( "This is your function that uses the document.forms[ index ] method." );

   alert( "\nYour form id: ( " + form[ 0 ].id + " ) -" ); // Lets output the form id >>>

   return false;
};
onload = function() { alert( "\n-IE mode is now activated." ); }
// ]]>
</script>
<script type="text/javascript" src="ex1.js"></script>
<script type="text/javascript" src="ex2.js"></script>

<!-- the ff.js must go on the last line of the loading sequence -->
<script type="text/javascript" src="ff.js"></script>
</head>
<body id="xhtml11">
<div id="main">
<center><form id="form-0" name="form-0" action="#" onsubmit="return somefunc();">
<div style="white-space : pre;">Name : <input type="text" id="txt" name="txt" size="15" maxlength="15" /> <input type="submit" value="- submit -" /></div>
</form></center>
</div>
</body>
</html>

- now submit your form in different modes: IE/Firefox ( and see which method will adapt ).

and that does it, your form is now availble under both conditions w/o spending to much time editing those lines over your 100's pages.

maximized and tested under toughest conditions using Opera—browser

Hi essential,
Thanks for posting the example . I will try ........

Cheers
--------
Siva

Hi essential,
The solution which was given by is fine,but here my problem i pages contains developed script using document.forms[0] as follows

<html>
<head>
    <script language="javascript">
       window.onload = function(()
       {
          document.forms[0].test.value="Test";
       }
  </script>

<body>
   <form action="test">
      <input type="text" name="test"/>
   </form>
</body>
</html>

The above code is dummy code, now this works fine in IE but in FireFox may or may not works, i need to handle this using document.getElementById('test').value="Test";

According your example
i need write one function for firefox and one function for IE which i understood, but it is difficult to write to all pages.

I hope u understood my problem.So please tell me how handle this type of situation without writing one more function for FireFox
which means i need make generalised when these type of situation
arised.

Thanks and Regards
----------------------
Siva kumar
</head>

Hi,

all it needs is a single .js file that will handle your form under firefox mode. The requirements is that you must include the ff.js in your 100 pages, via <script type="text/javascript" src="path/to/ff.js"></script> tag.

Hi sivakumarl,

here's your requested demo, showing how to handle the situation in your last post.

ff.js

if ( document.getElementById ) {
   window.onload = function() { alert( "NOT IE" );

   document.getElementById("form_0").test.value = "Test";

} };

dummyPage

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>http://www.daniweb.com</title>
<style type="text/css">
<!--

-->
</style>
<script type="text/javascript">
<!--
window.onload = function() {
   document.forms[ 0 ].test.value = "Test";

}


// --> 
</script>

<script type="text/javascript" src="./ff.js"></script>
</head>
<body>
<div>
<form id="form_0" name="form_0" action="#" onsubmit="return false;">
<div>
Test field: <input type="text" id="test" name="test" value="" size="20">
</div>
</form>
</body>
</html>

-essential

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.