下载最新版本的 GitHub 项目的链接
Download link for latest release of GitHub project
我正在尝试向我的网站添加指向项目最新 github 版本的下载链接。例如,链接 https://github.com/mongodb/mongo/archive/r3.0.0-rc7.zip 确实链接到最新版本(截至今天),但我不想在网站上对版本号进行硬编码。
我发现了几个关于这个问题的问题,使用 curl、ajax 或 php 来回答。
我尝试了使用github发布API的ajax的解决方案:
<!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>
但我无法正确调用 javascript 函数,正如我在上面的尝试(链接、链接 2 和 Link3)中所说的那样。我对javascript或ajax不是很熟悉,所以我不胜感激任何帮助;也许没有阿贾克斯有更简单的方法?
您正在加载一个 html 页面而不是他们的 REST API。获取标记的正确网址是 https://api.github.com/repos/mongodb/mongo/tags
你可能想在这里阅读更多关于 github api 的信息 - https://developer.github.com/v3/repos/
你的html可能看起来像这样:
<!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>
对我来说
,这很好用:
<!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>
如果您遇到未定义的问题,只需将$
更改为jQuery
,它应该都可以工作!
<!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>
这对我有用,希望对您有所帮助!
相关文章:
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 向每个堆叠条形图项目添加链接
- 如何使复选框项目返回超链接
- 下载最新版本的 GitHub 项目的链接
- 如何在两个不同数组中的项目之间建立链接
- 使用Jquery或CSS3隐藏链接时隐藏项目符号列表
- 单击相关滑块项目的链接
- 项目的类似Facebook的按钮不是链接
- 当某些链接相同时,当前所选项目
- 将JavaScript项目和Eclipse中的Java项目链接起来
- 我如何通过点击网页上的超链接来切换引导旋转木马中的项目
- 如何使用iron:router为集合中的每个项目创建一个单独的链接
- 我试图找到一种方法,让人们通过链接向他们的蒸汽购物车添加多个项目
- 使用jquery在嵌套列表中选择所有未链接的项目
- jQuery carousel -当到达列表上的最后一个项目时,禁用下一个/上一个链接
- JavaScript查找选定下拉项目的索引,传递到超链接
- 为什么Delete链接不能在这个Ruby on Rails项目中工作?
- 如何检测浏览器对项目链接的支持
- 动画调整大小通过点击一个链接,而不是项目被调整大小
- 从数据库和onclick项目之间的链接