2

I tried using the method described at w3 schools but it seems to only work in FireFox http://www.w3schools.com/jquery/jquery_ajax.asp

I used the example provided on the Try It

    <html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("div").load('test1.txt');
});
</script>
</head>
<body>

<div><h2>Let AJAX change this text</h2></div>

</body>
</html>

I just set it to run onload instead on after clicking. It did work there but when I make my own page it only works in FireFox.

Edit: Chrome just has security to prevent local file access it works on a server.

If anyone knows how this would work in older IE versions it would be a help

4
  • 1
    Are you executing this code in the context of a web server or just a static HTML file on your hard drive somewhere?
    – Tejs
    Apr 13, 2012 at 18:01
  • I took your code and tested it in Firefox, Chrome and IE (9) and it worked fine in all three.
    – Erikk Ross
    Apr 13, 2012 at 18:03
  • When you test, open your file in http://, not in file://. Of course you need a local webserver to do that. Apr 13, 2012 at 18:15
  • I figured out that my problem is actually on the IE 8 browser Chrome just has local file security that prevents doing it without a server. Does anyone know how to make it work in IE 8 or older? Apr 13, 2012 at 19:09

2 Answers 2

2

Your example works in the Chrome 18. You can try this way:

$(document).ready(function(){
    $.get('test1.txt', function(response) {
        $('div').html(response);
    })
});
3
  • this does the same thing doesn't it? yes it has the same results. Apr 13, 2012 at 19:10
  • works the same as the load function yes in modern browsers not in IE 8 Apr 13, 2012 at 19:15
  • I tried it in IE8 (8.0.6001.18702) and it works, BTW your code works too Apr 13, 2012 at 19:33
0

I recommend to take a look at jQuery AJAX function. It can use cache and it's better then using the "sub-alias" $.get.

3
  • Why should this be better, it's just a shorthand for $.ajax with default settings Apr 13, 2012 at 19:23
  • @RobAngelier You said it, default settings.. by default. Apr 13, 2012 at 19:28
  • It depends on the situation. If you don't need all the possible options, i would prefer the shorthand $.get Apr 13, 2012 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.