I am trying to add to my website a download link to the latest github release of a project. For example the link https://github.com/mongodb/mongo/archive/r3.0.0-rc7.zip does link to the latest release (as of today), but I do not want to hard-code the version number on the web site.
I found several questions about this issue, answers using curl, ajax or php.
I tried the solution using ajax which uses the github release API:
<!DOCTYPE html>
<HTML> <BODY>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
GetLatestReleaseInfo();
});
function GetLatestReleaseInfo() {
$.getJSON("https://github.com/mongodb/mongo/releases").done(function (json) {
var release = json[0];
var asset = release.assets[0];
var downloadURL = "https://github.com/mongodb/mongo/releases" + release.tag_name + "/" + asset.name;
$(".mongodb-download").attr("href", downloadURL);
});
}
</script>
<a href="GetLatestReleaseInfo();">Link</a>
<a href="" onclick="location.href=this.href+downloadURL;return false;">Link2</a>
<a href="" onclick="location.href=this.href+mongodb-download;return false;">Link3</a>
</BODY>
</HTML>
but I do not manage to call the javascript function correctly, as it seems in my tries above (Link, Link2 and Link3). I'm not very familiar with javascript or ajax, so I'd appreciate any help; maybe there is a simpler way without Ajax?
On GitHub.com, navigate to the main page of the repository. To the right of the list of files, click Releases. To copy a unique URL to your clipboard, find the release you want to link to, right click the title, and copy the URL. Alternatively, right click Latest Release and copy the URL to share it.
You're loading an html page instead of their REST API.The correct url to get the tags is https://api.github.com/repos/mongodb/mongo/tags
You may want to read more about github api over here - https://developer.github.com/v3/repos/
Your html could look like this :
<!DOCTYPE html>
<HTML> <BODY>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
GetLatestReleaseInfo();
});
function GetLatestReleaseInfo() {
$.getJSON("https://api.github.com/repos/mongodb/mongo/tags").done(function (json) {
var release = json[0];
var downloadURL = release.zipball_url;
$("#mongodb-download").attr("href", downloadURL);
});
}
</script>
<a id='mongodb-download' href="">Download latest mongo</a>
</BODY>
</HTML>
For me, this worked well:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready (function () {
$.getJSON("https://api.github.com/repos/mongodb/mongo/tags").done(function (data) {
$ ('#mongodb-download').attr ('href', data[0].zipball_url);
})
});
</script>
</head>
<body>
<a id="mongodb-download">Download the latest version of MongoDB</a>
</body>
</html>
If you are getting problems with undefined, just change the $
into jQuery
and it should all work!
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
jQuery(document).ready (function () {
jQuery.getJSON("https://api.github.com/repos/mongodb/mongo/tags").done(function (data) {
jQuery('#mongodb-download').attr ('href', data[0].zipball_url);
})
});
</script>
</head>
<body>
<a id="mongodb-download">Download the latest version of MongoDB</a>
</body>
</html>
This works for me, hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With