Strange "method undefined" in IE6 or IE7

Discussion in 'JavaScript' started by vinhtantran, Aug 12, 2009.

  1. #1
    I'm running some javascript in Vietnamese Wikipedia to hook the Vietnamese typing to website. The full page of code is here <http://vi.wikipedia.org/wiki/MediaWiki:ImportAVIM.js>, the resulting type interface can be seen in the left pane.

    However, when opening the website in IE6 and IE7 the first time, the browser always popup for script error which is in Line: 31, Char: 9, Error: 'method' is undefined, Code: 0. If I choose to continue this script, the problem never comes again.

    This problem could be passed if someone just care about its functionality, but I will be appreciate if anyone can show me what is the real problem here.

    Thank you for any reply.

    PS: The same problem happens in IE8, too, with a more specific url <http://localhost/wikipedia/index.php?title=MediaWiki:ImportAVIM.js&action=raw&ctype=text/javascript>
     
    Last edited: Aug 12, 2009
    vinhtantran, Aug 12, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Have you at the very least set a doctype at the top of the code? Some javascript routines (such as lightview) won't work in IE unless there's an xhtml 1.0 doctype defined in the top. Without seeing what's on line 31 or the site affected thats the only thing I can think of.

    Assuming the site is xhtml and uses a unicode charset the top of the html would be like such:

    
    <?xml version="1.0" encoding="UTF-8"?>
    <!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">
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    
    Code (markup):
    Course if the page is php you may need to echo out the first line to avoid problems.
     
    kblessinggr, Aug 12, 2009 IP
  3. NoamBarz

    NoamBarz Active Member

    Messages:
    242
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #3
    I think you're calling a method before it loads in the browser. try checking for typeof method != "undefined" and timeout until it loads
     
    NoamBarz, Aug 12, 2009 IP
  4. vinhtantran

    vinhtantran Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Wikipedia is validated as XHTML Transitional, I have checked with W3C. <http://validator.w3.org/check?verbose=1&uri=http%3a%2f%2fvi.wikipedia.org%2fwiki%2fTrang_Ch%u00ednh>
     
    vinhtantran, Aug 13, 2009 IP
  5. vinhtantran

    vinhtantran Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    What exactly the method you mentioned?
     
    vinhtantran, Aug 13, 2009 IP
  6. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #6
    HIMHTML = '<input id="him_auto" name="viet_method" type="radio" onclick="setMethod(0);"' + (method == 0 && on_off==1?'CHECKED':'') + '/>&nbsp;<label class="radio" for="him_auto">Tự động</label> <small>[F9]</small><br />';
    PHP:
    method == 0 -> method being undefined here

    you seem to be using a dom loader called addDomHook() which presumably allows you to add events onload.

    there are - however, instances where IE would release the domready before it really is ready... you should use a new / recent version of a js framework (mootools, jquery, etc) that has a better domready - or you can add a slight delay...

    there are no guarantees anyway, sometimes i change my ready event from 'domready' to 'onload' for IE only because the other attempts to guess when elements are free to modify can fail.
     
    dimitar christoff, Aug 13, 2009 IP
  7. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #7
    But you're not loading the javascript on wikipedia you're loading it on your own site. js files don't have doctypes, the html files that load them do, in this case your own site.

    someone mentioned the method not being loaded yet... in IE <script src="script.js" type="text/javascript" defer="defer"></script> the defer="defer" in *most* cases will cause IE to wait til the document is loaded prior to executing the script.
     
    kblessinggr, Aug 13, 2009 IP
  8. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #8
    interesting... can you provide an example where it would _not_ wait? would document.writes / dom interaction cause it to ignore it?

    it kind of defeats the purpose of the attribute - then again, people can defer stupid scripts so it can't be trusted 100%... hrm. cheers for any extra info you may provide :)
     
    dimitar christoff, Aug 13, 2009 IP
  9. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Well if you notice most JQuery examples are often provided in $.ready() blocks making sure the DOM is ready, so browsers must have had an inherit habit of doing that or the frame works like Jquery, Prototype, Dojo and so forth wouldn't be showing everything in those blocks. Any code you load in a script gets executed as soon as the browser can run it, and in most cases the DOM is ready, but not always, especially when the javascript is being loaded off site and as such creates an extra delay in loading.
     
    kblessinggr, Aug 13, 2009 IP