dcsimg
www.webdeveloper.com
Results 1 to 10 of 10

Thread: XMLHttpRequest works in Safari, but not Chrome, Firefox or Opera

  1. #1
    Join Date
    Nov 2009
    Posts
    22

    XMLHttpRequest works in Safari, but not Chrome, Firefox or Opera

    I have this working perfectly in Safari, but nowhere else for some reason...

    Code:
    	var txtFile = new XMLHttpRequest();
    		txtFile.open("GET", "http://www.personal.psu.edu/pzb4/js_captions/3872452-captions-kTaHhmTs.vtt", true);
    		txtFile.onreadystatechange = function() {
    			var contents;
      			if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
        			if (txtFile.status === 200) {  // Makes sure it's found the file.
          			allText = txtFile.responseText;

  2. #2
    Join Date
    Dec 2012
    Posts
    1,447
    1. Obviously your code is incomplete. Please post the complete version.
    2. I recommend using jQuery which is made to hide cross browser issues.

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    4,982
    Here --> https://www.w3schools.com/xml/ajax_examples.asp good starting point with a couple of examples on how to use AJAX and AJAX with a function call back.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  4. #4
    Join Date
    Nov 2009
    Posts
    22
    Thanks. I'll see if I can get that worked into my code. Sample code is helpful!

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    4,982
    Quote Originally Posted by Pat the Rat View Post
    Thanks. I'll see if I can get that worked into my code. Sample code is helpful!
    Well the one sample code is a ready to go fully working example, the best one is the one where you can have a call back function called and pass the return value as a parameter and it then gives you an external function to do whats needed, like update a page element or whatever you program the function to do...
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  6. #6
    Join Date
    Nov 2009
    Posts
    22
    yes, i tried that page on all the browsers and it seemed to work fine. on monday i'll try to implement it. thanks again!

  7. #7
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    4,982
    Well its good to hear, however, if you are going to get errors and problems with it because it may not be cross browser compatible or friendly, you could look on the MDN (Mozilla [firefox people] Developer Network) for their full on bells and whistles AJAX library which is pretty comprehensive and can integrate with minimal tweaking. It would be the better long term option.

    About 1/2 to 2/3rds way down the page will find example scripts https://developer.mozilla.org/en-US/...XMLHttpRequest that would be worth noting how its set up and that should tell you enough on how to integrate it in to your form / site.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  8. #8
    Join Date
    Nov 2009
    Posts
    22
    Running this sample code, it appears that only Safari can load the text file I need to load, perhaps because it isn't an xml file, but plain text. http://www.personal.psu.edu/pzb4/js_...s-kTaHhmTs.vtt
    Also tried jquery with the same results...
    <script>
    $( "#success" ).load( "http://www.personal.psu.edu/pzb4/js_captions/3872452-captions-kTaHhmTs.vtt", function( response, status, xhr ) {
    if ( status == "error" ) {
    var msg = "Sorry but there was an error: ";
    $( "#error" ).html( msg + xhr.status + " " + xhr.statusText );
    }
    });
    </script>
    Works in safari but not the other 3.

  9. #9
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    4,982
    You can use AJAX but you have to remember that it is domain specific which means your webpage and ajax call should be on the same domain.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  10. #10
    Join Date
    Nov 2009
    Posts
    22
    ah that was it. i placed it in the same folder as the html file and linked to it there and it works in all (using the original script I had). Thanks!

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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