Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Strange code execution order

  1. #1
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default Strange code execution order

    Can someone please explain why the bit in red:
    Code:
    function menu(parent, menuarr, layer) {
    	parent.style.backgroundColor = "silver";
    	theMenu = document.getElementById("menu");
    	if(theMenu.innerHTML.indexOf('<p id="menulayer' + layer + '">') == -1) {
    		theMenu.innerHTML += "<p id=\"menulayer" + layer + "\">";
    	} else {
    		theMenu = document.getElementById("menulayer" + layer);
    	}
    	for(i = 0; i < menuarr.length; i++) {
    		if(menuarr[i].indexOf('::') != -1) {
    			title = menuarr[i].substring(0, menuarr[i].indexOf('::'));
    			url = menuarr[i].substring(menuarr[i].indexOf('::') + 2, menuarr[i].length);
    		} else {
    			title = menuarr[i].substring(0, menuarr[i].indexOf(';;'));
    			url = menuarr[i].substring(menuarr[i].indexOf(';;') + 2, menuarr[i].length);
    			url = "javascript:void(0);\" onclick=\"menu(this, " + url + ", " + (layer + 1) + ");";
    		}
    		theMenu.innerHTML += "<a href=\"" + url + "\">" + title + "</a>";
    		if(!(i == menuarr.length - 1)) theMenu.innerHTML += "&nbsp;&bull;&nbsp;";
    	}
    	theMenu.innerHTML += "</p>";
    	theMenu.style.display = "inherit";
    }
    ... is being executed before the bit in green?
    It's been bugging me for ages; probably just needs a different viewpoint.

    Thanks in advance,
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    No doubt you're expecting this answer: it can't be. The loop, its block statement, and all side effects must be completely evaluated before the assignment statements after it are executed. This only way this could be different is if the global setTimeout function is used, or you're dealing with a very broken interpreter.

    Without a bare-minimum demonstration though, it's very difficult to explain why you're seeing what you're seeing.

    Mike

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The output was appearing as "<p></p>menu" instead of "<p>menu</p>".
    I think it's because the innerHTML was temporarily invalid, so the browser (Firefox) quickly stuck a </p> in to make it valid. I solved it by putting everything into a variable, then transferring it to innerHTML all at once.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I too have run into situations where innerHTML had to be written at once rather than using the += method. Not sure of why this happens sometimes and not others.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I just did some tests and IE6 will throw an unknown error if you try that with a paragraph. IE will accept a span though but, closes it twice - once after it is written without a closing tag and again when you add the closing tag. FF will close it just as you describe and then ignore the following command to close the paragraph or span as extraneous.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Well, that's that off my mind Thanks to all for the effort.
    Now for the next problem: does anyone know why this page throws a "Could not get display property. Invalid argument." error on accessing the menu in IE? Nothing else seems to mind (Firefox, Mozilla, Opera, Epiphany, Konqueror).

    P.S. Yes I know it's awful, it's only a rough draft
    Last edited by Twey; 07-04-2005 at 06:52 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Yeah, IE's script error reporting leaves much to be desired. I notice you have alot of '!=' where '!==' would be more correct. I'd try replacing those first to see what happens. The only other suggestion I would have is that since there are only 4 written calls to access the display property in the script, try commenting them out one at a time to (hopefully) see where IE chokes.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I've never heard of !==.
    What is the difference from !=? (opposite of ===?)
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    I've never heard of !==.
    What is the difference from !=? (opposite of ===?)
    Yes, !== and === are opposites (as are != and == of course).

    You probably know the general difference of the two forms from PHP (== type converts, whilst === doesn't).

    Mike

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You learn something every day. I always used !(a === b) when that was necessary, and avoided it otherwise.

    EDIT:
    Found problematical line:
    Code:
    45: document.getElementById("menu").style.display = "inherit";
    ... but can't see what's wrong with it. Have changed offending <div> to a <span>, but still no joy.
    Last edited by Twey; 07-05-2005 at 09:29 AM. Reason: After tests
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •