1

So I am building my website using HTML/CSS. I would like to know what is the easiest way to check if a URL is availble?

For example, I have a table with a cell that shows a link to some 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>

In this cell, there is a link to some url. I want to show the link as URL (a href) only if http://{{ server.ServerName }}.ilo.lab.dba.co.il is working using http! (server.ServerName is a variable running in a for loop giving different DNS URLS) If it's not, I will show just a text, or not show it all...

I have found some functions in Javascript that return true and false, but I dont know how to call the function in my html. Like, I was thinking of doing if (link works) : show link with a href, else: show just the string...

Is it possible using Javascript function? If so, what's the function and how do I call it?

An example for a function I found:

How can I use and test it in my code? using href if it's true?

  function UrlExists(url)
    {
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();
    return http.status==200;
    }
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
EilonA
  • 361
  • 5
  • 17
  • 3
    "I have found some functions in Javascript that return true and false" what function? and why did you tag `django`, `php` and `css`? – Omar Einea Jan 02 '18 at 11:28
  • You can make an fetch call on each url and check if its responseheader has code 200 and then add a class active to the td if its 200 or hide it by default – orangespark Jan 02 '18 at 11:30
  • The answer to whether a link written as `...` is available is: Probably not, unless you have a resource in the same directory that has the name `10.0.5.1`. Perhaps you meant `http://10.0.5.1`, `https://10.0.5.1`, or `//10.0.5.1`. – T.J. Crowder Jan 02 '18 at 11:31
  • @orangespark: Only if it's in the same origin or supports CORS. – T.J. Crowder Jan 02 '18 at 11:31
  • @OmarEinea Example for a function I have found that I don't know how to check if it's working: function UrlExists(url) { var http = new XMLHttpRequest(); http.open('HEAD', url, false); http.send(); return http.status==200; } I tagged django because my web is in django, so maybe there is a solution from the backend side.. also, maybe php could have a function.. – EilonA Jan 02 '18 at 11:33
  • well that is a case which comes later right now i am suggesting a method to check. @T.J.Crowder – orangespark Jan 02 '18 at 11:33
  • just use a fetch api why do u need to write so much code with xhr – orangespark Jan 02 '18 at 11:34
  • @orangespark How do I do this method? – EilonA Jan 02 '18 at 11:34
  • @orangespark: `fetch` has the same SOP limitations as XHR (of course). – T.J. Crowder Jan 02 '18 at 11:35
  • fetch api is inbuilt in javascript all the latest browsers support it. It has a success and error function too. so in the success u can add a class which has display:block and in error u do nothing also by default give display:none to all the tds – orangespark Jan 02 '18 at 11:36
  • @T.J.Crowder ya i agree but xhr implementation is less readable code you have to agree to that. – orangespark Jan 02 '18 at 11:37
  • @EilonAshkenazi: I'd say you probably use `UrlExist` to check the existence of any url. Could you post here how you render the table's content. I need a bit more details of your code to assist precisely. – Tung Jan 02 '18 at 11:51
  • @Tung: I added the whole table code now.. The backhand is in Django and the ServerName is a varialbe I'm using to generage in a for loop details about each server and the ILO IP I am using is a DNS contains the servername – EilonA Jan 02 '18 at 12:00
  • @Tung any suggestion? – EilonA Jan 02 '18 at 14:07
  • @EilonAshkenazi: I am looking for a way to check the existence of a given URL in Python as suggested by @T.J. Crowder and will let you know soon tonight. In my mind at the time being, we can either check server's availability using Python-based code or update the `td` of ServerName after rendering the template. The latter could allow you to use JavaScript code you found. – Tung Jan 02 '18 at 14:17
  • @Tung Thank you please keep me updated – EilonA Jan 02 '18 at 16:35

2 Answers2

2

Assuming your page isn't hosted on http://10.0.5.1, there's no general-purpose way to do this from the browser unless the target servers allow it, and separately it's probably not ideal to do so.

If the origin of your target pages isn't the same as the origin of the page, the Same Origin Policy kicks in and prevents your using ajax (XMLHttpRequest, fetch) to check the links. You could work around that if you can implement Cross-Origin Resource Sharing on all of the target servers, telling them to give your page access. But again: Requesting all of the resources linked on the page, even with a HEAD request, is probably not ideal.

You've tagged your question with PHP. You could certainly use PHP to check the links before returning the page. This question's answers show how to do a HEAD request for a URL.

You've also tagged Django, a Python server-side framework. You can do a HEAD request with Python, too; this search turned up this question and its answers.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Is using PHP the best and easist way then? Because I don't know how to use PHP at all and I don't have any PHP code in my page. – EilonA Jan 02 '18 at 11:38
  • @EilonAshkenazi: Then why did you tag the question PHP? In any case: Yes, doing it from the server (whether with PHP or some other server-side technology) is probably best, and it's also probably best to cache the result for X seconds/minutes so you're not doing it every single time someone requests your page. – T.J. Crowder Jan 02 '18 at 11:39
  • Because after looking in Google I have seen solutions using AJAX,PHP and Javascript. The only code I use now in my website is HTML but I will add the easiest, the best solution if it requires PHP or Javascript or whatever that works. – EilonA Jan 02 '18 at 11:46
  • I can't implement anything on the target servers.. It's an ILO IP of an HP server – EilonA Jan 02 '18 at 11:48
  • @EilonAshkenazi: If the backend is Django, it's Python; I've updated the answer to address that. – T.J. Crowder Jan 02 '18 at 12:03
  • Crowder : I edited my original post to get closer to the answer.. I know have an error of - Could not parse the reminder: '(server.IP)' from 'checkUrlAvailable(server.IP)' – EilonA Jan 02 '18 at 17:40
  • @EilonAshkenazi: SO questions are not meant to be moving targets. Your original question has been answered. Instead of editing it into a new question ("What's wrong with my Python code doing X"), you should leave it as-is and post a *new*, targeted question about the problem you're *now* having. I've rolled back the edit. – T.J. Crowder Jan 02 '18 at 17:46
  • 1
    Thank you! I will do that now – EilonA Jan 02 '18 at 17:47
1

Due your application backend is Django, I would recommend you using Python to check whether an http based url is available or not.

According to @T. J. Crowder suggested, track of the links/questions to implement the mothod checkUrlAvailable(url) with a URL as the argument.

I've found the documentation of Built-in template tags and filters showing how to apply filters in Django template.

All in all, we can combine all together like the following:

<td width="100%"><center>
   {% if checkUrlAvailable(server) is True %}
      <a href="//{{ server.ServerName }}.ilo.lab.dba.co.il"> http://{{ server.ServerName }}.ilo.lab.dba.co.il </a>
   {% else %}
      http://{{ server.ServerName }}.ilo.lab.dba.co.il
   {% endif %}
</center></td>

The problem I am having is to check an non-existing server/url with Python. I will update this issue soon once it would be resolved.

To write checkUrlAvailable in Django template, take a look at the documentation. The idea is to build a customised filter to check the existence of a given URL. The pseudo code could be looked like the following but I argue to dive into these answers:

import requests

def checkUrlAvailable(url):
    resp = requests.head(url)
    if resp.status_code == 200:
       return True
    else
       return False

I just borrowed the snippet code from stackoverflow's link.

(to be updated soon)

Tung
  • 1,579
  • 4
  • 15
  • 32