How can I import a text file into javascript from an external website using jquery $get()?
Posted
by Edward Tanguay
on Stack Overflow
See other posts from Stack Overflow
or by Edward Tanguay
Published on 2010-04-28T03:50:07Z
Indexed on
2010/04/28
3:53 UTC
Read the original article
Hit count: 515
When the button in the following script gets clicked, it should load in the contents of the file "http://tanguay.info/knowsite/data.txt" and display it on the screen.
What is the correct syntaxt so that the .get() function retrieves the data from the external website and puts it in #content?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript"
src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1.3.2");
google.setOnLoadCallback(function() {
$('#toggleButton').click(loadDataFromExernalWebsite);
});
function loadDataFromExernalWebsite() {
$('#content').html('new content');
//$.get("http://tanguay.info/knowsite/data.txt", function(data) { alert(data); }, );
}
</script>
</head>
<body>
<p>Click the button to load content:</p>
<p id="content"></p>
<input id="toggleButton" type="button" value="load content"/>
</body>
</html>
© Stack Overflow or respective owner