OrangeKing89
OrangeKing89

Reputation: 704

How do you use JQuery to load HTML fragment from another file?

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

Upvotes: 2

Views: 2174

Answers (2)

David B&#233;langer
David B&#233;langer

Reputation: 7438

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

Upvotes: 0

Vitalii Petrychuk
Vitalii Petrychuk

Reputation: 14255

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

$(document).ready(function(){
    $.get('test1.txt', function(response) {
        $('div').html(response);
    })
});

Upvotes: 2

Related Questions