www.webdeveloper.com
Results 1 to 11 of 11

Thread: Problem with onerror (beginner)

  1. #1
    Join Date
    Nov 2009
    Posts
    109

    Problem with onerror (beginner)

    Hi,
    Somebody can help me to find the error in this code? It doesn’t work correctly on all browser. I tried with opera, chrome, ie8, firefox, safari and netscape.
    My code must write on a second window the report of the onerror command.
    CODE:
    Code:
    <script>
     // handler
     window.onerror = myOnError;
     
     msgArray = new Array();
     urlArray = new Array();
     lnoArray = new Array();
     
     function myOnError(msg, url, lno) {
      msgArray[msgArray.length] = msg;
      urlArray[urlArray.length] = url;
      lnoArray[lnoArray.length] = lno;
      return true;
     }
    
     function displayErrors() {
      win2 = window.open('','window2','scrollbars=yes');
      win2.document.writeln('<B>Error Report</B> <P>');
      
      for (var i=0; i < msgArray.length; i++) {
       win2.document.writeln('<B>Error in file:</B> ' + urlArray[i] + '<BR> ');
       win2.document.writeln('<B>Line number:</B> ' + lnoArray[i] + '<BR>');
       win2.document.writeln('<B>Message:</B> ' + msgArray[i] + '<P>');
      }
      win2.document.close();
     }
    </script> 
    
    <form>
    <input type="button" value="Questo bottone ha un errore di sintassi" onClick="alert('unterminated string)" 
    name="button"> <p> <input type="button" value="Visualizza l'errore" onClick="displayErrors()" name="button2">
    </form>
    MY JAVASCRIPT GUIDE:
    http://javascript.html.it/guide/lezi...degli-oggetti/

  2. #2
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    What is the problem? Saying it doesn't work does not tell us anything.

    Doctor: Why are you here?
    Patient: I am sick.
    Doctor: What is the exact problem?
    Patient: I am sick, DUH!

    If I pull out my magic crystal ball, I would say pop up blocker.

    Eric

  3. #3
    Join Date
    Nov 2009
    Posts
    109
    Ok, I thought that it was simple to understand. The code must open a second windows where I find three report:
    1) The url where my file is placed on my computer.
    2) The line where the mistake is.
    3) The type of the mistake.
    When I use Firefox I read:
    Error Report
    Error in file: file:///E:/DATI&#37;20PERSONALI%202009/PROGETTAZIONE%20SITI%20WEB/07%20-%20JAVASCRIPT/prova005a.html
    Line number: 1
    Message: unterminated string literal
    Error in file: file:///E:/DATI%20PERSONALI%202009/PROGETTAZIONE%20SITI%20WEB/07%20-%20JAVASCRIPT/prova005a.html
    Line number: 1
    Message: unterminated string literal
    Error in file: file:///E:/DATI%20PERSONALI%202009/PROGETTAZIONE%20SITI%20WEB/07%20-%20JAVASCRIPT/prova005a.html
    Line number: 1
    Message: unterminated string literal
    But the mistake is not placed into the first line!
    When I use IE8 I read:
    Error Report
    Error in file: file:///E:/DATI%20PERSONALI%202009/PROGETTAZIONE%20SITI%20WEB/07%20-%20JAVASCRIPT/prova005a.html
    Line number: 56
    Message: Costante String senza terminazione
    But I should read three time this report!
    When I use the others browser I read only:
    Error Report
    So I would like to under stand how to have a code that works on all browser and like I can have at least one code that works on 1 browser!
    Thanks
    Marco

  4. #4
    Join Date
    Nov 2009
    Posts
    109
    Can you understand my problem? Can you understand my poor English?

  5. #5
    Join Date
    Nov 2009
    Posts
    109
    Is there somebody?

  6. #6
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    msgArray.length is zero (the loop never runs), so the window is opened with only the text "Error Report"

  7. #7
    Join Date
    Nov 2009
    Posts
    109
    Quote Originally Posted by Fang View Post
    msgArray.length is zero (the loop never runs), so the window is opened with only the text "Error Report"
    Therefore? Why with Firefox work?

  8. #8
    Join Date
    Dec 2008
    Posts
    488
    I'm surprised no one mentioned this before... onerror only works with IE and Firefox. It's not supported by other browsers. The only way around it is to incorporate try{} catch(e){} statements into your code. I know that's not always possible, especially considering some of your code might be offsite tracking scripts or something similar.

  9. #9
    Join Date
    Nov 2009
    Posts
    109
    Quote Originally Posted by jamesbcox1980 View Post
    I'm surprised no one mentioned this before... onerror only works with IE and Firefox. It's not supported by other browsers. The only way around it is to incorporate try{} catch(e){} statements into your code. I know that's not always possible, especially considering some of your code might be offsite tracking scripts or something similar.
    Ok, I understand but I have a problem with your last sentence. What is the mean of "might be offsite tracking scripts or something similar"? My reading is not very good. Can you rewrite it in different way?

    Could you write a code that it works on all browsers?

    Thanks

  10. #10
    Join Date
    Dec 2008
    Posts
    488
    Quote Originally Posted by esacerbare View Post
    Ok, I understand but I have a problem with your last sentence. What is the mean of "might be offsite tracking scripts or something similar"? My reading is not very good. Can you rewrite it in different way?

    Could you write a code that it works on all browsers?

    Thanks
    Sure. What I mean is that if you were to use try{} catch(e){} statements in all your scripts, you could catch important errors. But you would not be able to do this if you're using a script that comes from another website, because you would not be able to change their code. Tracking scripts were just an example. A tracking script might be Google Analytics or something like that.

    That make more sense?

  11. #11
    Join Date
    Nov 2009
    Posts
    109
    Ok, thanks very much!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles