/    Sign up×
Community /Pin to ProfileBookmark

Problem creating menus onload

Hello,

I am newbie to Javascript so I apologize if this is a stupid question (in keeping with the forum etiquette, I did a search as well but could not find anything that seemed relevant).

My problem is that I am trying to dynamically create some menus on a site but it only works part of the time in Firefox (but always if I debug using firebug) and never in IE.

I use the following to call the code on startup:

[CODE]window.onload = downloadXml(“menu.xml”, createMenus);[/CODE]

Where downloadXml is a function using a XMLHttpRequest to get access to my XML file and createMenus is a function that grabs a particular div tag in the HTML DOM and then appends children based on elements in the XML DOM.

Is it possible there is some sort of timing problem that prevents Firefox from grabbing HTML/XML elements before they have been put in the DOM?

For full code please see
[url]http://anataphoto.com/scripts/ajaxframework.js[/url]
[url]http://anataphoto.com/scripts/tabmenu.js[/url]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@voidvectorJan 31.2009 — You are using window.onload incorrectly. What you did is calling downloadXml immediately (while the page is still loading) and assigning the return value (which is undefined) to onload.

It should be
<i>
</i>window.onload = function () // define an anonymous function to be run during the "onload" event
{
downloadXml("menu.xml", createMenus);
}
Copy linkTweet thisAlerts:
@MaikuauthorJan 31.2009 — Wow, thank you so much.

The sad thing is that part I copied from a book. But your explanation makes perfect sense: javascript wouldn't know that I want to set that to the event rather than call that function and get the result.

Now I just need to figure out what's breaking in IE and I'll be set!
Copy linkTweet thisAlerts:
@voidvectorFeb 01.2009 — You have a typo in you XHR code, "XMLHTPP" should be "XMLHTTP"
Copy linkTweet thisAlerts:
@MaikuauthorFeb 01.2009 — Thanks. Seems I should do more spellchecking before running code.

It seems that is not the problem I was hitting though. As I found in another post on this site apparently IE7 and up use the native XMLHTTPRequest object instead of ActiveX.

So my code was getting the native object but IE does not support overrideMimeType so it was dying on that (and another spot where I had attributes.getNamedItem('stuff') instead of attributes.getNamedItem('stuff').nodeValue).

So to fix the first error it seems we should use:

[CODE] if(window.XMLHttpRequest)
{
XMLHttpRequestObject = new XMLHttpRequest();

// IE7+ uses native object but does not support overrideMimeType
if(XMLHttpRequestObject.overrideMimeType)
{
XMLHttpRequestObject.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject)
{
XMLHttpRequestObject = ActiveXObject("Microsoft.XMLHTTP");
}
[/CODE]
×

Success!

Help @Maiku spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...