Welcome to WebmasterWorld Guest from 107.22.14.254

Forum Moderators: open

Message Too Old, No Replies

Cross browser javascript problem

Javascript function works in firefox but not in IE

     
5:40 am on Apr 16, 2009 (gmt 0)

5+ Year Member



Hi all,
I have recently joined the web master's community and i'm a beginner to javascripts.
I have a javascript function which works fine firefox but not in IE, an error "object doesn't support this property or method" occurs when the modify functionality is used. I would appreciate any help on this. I'm attaching the script function and the jsp code here.

JSP code:
<div class="ico">
<a href="#" title="modify" onclick="submitForm()"> <img src="../images/ico_natiz.gif" alt="modifica" border="0"> </a>
</div>

Javascript code:
function submitForm(nameId){
if(nameId){
document.getElementById(nameId).onsubmit();
document.getElementById(nameId).submit();
}
else{ document.getElementById("submitForm").onsubmit(); document.getElementById("submitForm").submit();
}
}

Thanks for the help in advance:)

6:52 am on Apr 16, 2009 (gmt 0)

10+ Year Member



Is the forms id nameId? if it is It should be
<a href="#" title="modify" onclick="submitForm('nameId')">

The function "submitForm(nameId)" requires something to be passed to it.

Also are you checking if a certain id exists?

Then instead of

if(nameId){

it should be something like

if(document.getElementById(nameId) != null)

10:41 am on Apr 16, 2009 (gmt 0)

5+ Year Member



I have tried both suggestions made by you, but when only 'nameId' is passed to the script function the modify option is not working in both the browsers:(

When the 2nd suggestion for checking the id "if(document.getElementById(nameId) != null)" is implemented, it works in firefox but not in IE.

Is there any other changes that can be done to get it working in IE too? I have been struggling to get ride of this bug for over a week now.

1:22 pm on Apr 16, 2009 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld! Using JavaScript to submit your form will break your functionality for people with JavaScript disabled, so you might want to reconsider your approach. That is... use a real button instead of a link.

As for you function, it's attempting to take a string input and get the form element with that ID. If no string was input, it defaults to getting a form with the id of "submitForm". You can reduce the number of calls to document.getElementById to make it more efficient. Also, the call to the form's onsubmit() handler will cause an error if that handler has not been set. So that could be rewritten:


function submitForm(formId) {
// Get the form element using the passed in formId
// or the default 'submitForm' if no id passed in.
var frm = document.getElementById(formId ? formId: 'submitForm');
// Perform a check to make sure the form exists
if (frm != null) {
// Perform a check to make sure the form's
// onsubmit event handler was set
if (frm.onsubmit != null) {
frm.onsubmit();
}
frm.submit();
}
}

Note, this is very much dependent on event 'handlers' (vs. event 'listeners').

 

Featured Threads

Hot Threads This Week

Hot Threads This Month