【发布时间】:2018-01-02 11:26:13
【问题描述】:
所以我正在使用 HTML/CSS 构建我的网站。 我想知道检查 URL 是否可用的最简单方法是什么?
例如,我有一个表格,其中包含一个显示指向某个 URL 的链接的单元格 -
<table class="table table-hover table-bordered table-condensed" cellspacing="0" width="1300" id="ServerTable">
<thead>
<tr>
<th><center> #</center></th>
<th width="100%"><center> Server Name </center></th>
<th><center> Owner </center></th>
<th><center> Project </center></th>
<th width="100%"><center> Description </center></th>
<th width="100%"><center> IP Address </center></th>
<th width="100%"><center> ILO </center></th>
<th><center> Rack </center></th>
<th><center> Status </center></th>
<th><center> Actions </center></th>
</tr>
</thead>
<tbody>
{% for server in posts %}
<tr>
<div class ="server">
<td></td>
<td width="100%"><center>{{ server.ServerName }}</center></td>
<td width="100%"><center>{{ server.Owner }}</center></td>
<td width="100%"><center>{{ server.Project }}</center></td>
<td width="100%"><center>{{ server.Description }}</center></td>
<td width="100%"><center>{{ server.IP }}</center></td>
<td width="100%"><center><a href="//{{ server.ServerName }}.ilo.lab.dba.co.il"> http://{{ server.ServerName }}.ilo.lab.dba.co.il </a></center></td>
<td width="100%"><center>{{ server.Rack }}</center></td>
<td width="100%"><h4><span class="badge badge-success">Online</span></h4></td></center>
在这个单元格中,有一个指向某个 url 的链接。 如果 http://{{ server.ServerName }}.ilo.lab.dba.co.il 正在使用 http,我想将链接显示为 URL(a href)only! (server.ServerName 是一个在 for 循环中运行的变量,提供不同的 DNS URL) 如果不是,我将只显示一个文本,或者不显示全部...
我在 Javascript 中找到了一些返回 true 和 false 的函数,但我不知道如何在我的 html 中调用该函数。 就像,我正在考虑做 if (link works) : show link with a href, else: show just the string...
是否可以使用Javascript函数?如果是这样,函数是什么以及如何调用它?
我找到的函数示例:
如何在我的代码中使用和测试它?如果是真的使用href?
function UrlExists(url)
{
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status==200;
}
【问题讨论】:
-
“我在Javascript中找到了一些返回真假的函数”什么函数?你为什么要标记
django、php和css? -
您可以对每个 url 进行 fetch 调用,并检查其 responseheader 是否有代码 200,然后将一个活动的类添加到 td 如果它的 200 或默认隐藏它
-
对于写成
<a href="10.0.5.1">...</a>的链接是否可用的答案是:可能不可用,除非您在同一目录中有一个名为10.0.5.1的资源。也许您的意思是http://10.0.5.1、https://10.0.5.1或//10.0.5.1。 -
@orangespark:只有同源或者支持CORS。
-
@OmarEinea 一个函数的例子我发现我不知道如何检查它是否工作:function UrlExists(url) { var http = new XMLHttpRequest(); http.open('HEAD', url, false); http.send();返回http.status==200;我标记了 django 因为我的网站在 django 中,所以也许后端有一个解决方案.. 另外,也许 php 可以有一个函数..
标签: javascript php jquery ajax django