homepage Welcome to WebmasterWorld Guest from 54.242.204.141
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Cross browser javascript problem
Javascript function works in firefox but not in IE
Naremk




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

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:)

 

outrun




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

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)

Naremk




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

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.

Fotiman




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

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').

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved