so i'm trying to use an API to get a random word (http://randomword.setgetgo.com) and then use it to store the random word as a variable in javascript and then print that variable to the screen. My Code is
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0.min.js">
var word = $.get("http://randomword.setgetgo.com/get.php");
document.WriteIn(word);
</script>
However nothing is being written to the HTML document.
Could anyone help explain this a little bit and point me in the write direciton to get this working? I'm just starting to learn web development.
Thanks.
You need to put your code in <script>
tags.
You also need to make sure that you have included the jQuery library.
Put your scripts between <\script> tags, usually in the -section of your document. Then call it from the point you like.
I recommend looking at some tutorials, since these are the very basics that bug you
Why don't you put the random word in a div
?
Like so:
$("#theDiv").text(word);
And you need to put your code in a separate <script>
element that does not have a src
attribute.
You can't both set the [src]
attribute and the contents of the <script>
tag. If you need two scripts, you need to use two script tags:
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0.min.js"></script>
<script>
var word = $.get("http://randomword.setgetgo.com/get.php");
document.WriteIn(word);
</script>
Additionally, $.get
runs asynchronously and will not return the contents of the url request.
document.WriteIn
is not a function, unless you've defined it somewhere.
If you just need to dump the contents of the GET
request after the executing script, you could use:
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0.min.js"></script>
<script>
(function () {
var $script;
$script = $('script').last();
$.get('http://randomword.setgetgo.com/get.php', function (data) {
$script.append(data);
});
}());
</script>
Thre are multiple problmes...
so
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0.min.js"></script>
<script>
$.get("http://randomword.setgetgo.com/get.php", function(content){
$('body').append(content)
});
</script>