6

I have a button which links to another page. Before I click that link, I want to check if that page currently returns an error or if it is not existing. How can I code this? Is this possible? I want to disable the link if it is not existing or if it returns an error.

skynet
  • 9,898
  • 5
  • 43
  • 52
newbie
  • 14,582
  • 31
  • 104
  • 146

4 Answers4

3

You could use ajax to load the page and then parse it to see if there's an error before you then redirect the user. I would also include some kind of note to the user as to why the link isn't working.

Jordan
  • 183
  • 1
  • 10
  • how? can you give me an example. I will appreciate it very much. Thank you. – newbie Aug 17 '12 at 21:30
  • @newbie There are examples in the documentation. http://api.jquery.com/jQuery.ajax/ – Kevin B Aug 17 '12 at 21:31
  • not exactly what you want, but might be something to start with! http://stackoverflow.com/questions/2493974/how-callback-a-function-on-404-on-json-ajax-request-with-jquery – ablm Aug 17 '12 at 21:32
  • according to the jQuery.ajax api, "**most** "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol", "Script and JSONP requests are not subject to the same origin policy restrictions." Here the question was only if the page in question exists or returns an error (the success attribute). – Robin Manoli Aug 17 '12 at 22:19
3

Note that I'd do this on server side. However as your tags are related to jquery, I post a solution you may try with jquery.

using jquery you can:

var link = $('#id_of_your_a_element');
$.ajax(
    url: link.attr('href'),
    error: function() { 
        link.hide();
    }
);

EDIT

As ribot noted, this example does only hide the link. If you want to disable it you can prevent the default behavior when user clicks the link:

link.click(function(e) {
    e.preventDefault();
});

or alternatively just remove the href attribute:

link.removeAttr('href');
Savas Vedova
  • 5,622
  • 2
  • 28
  • 44
2

As someone had asked, what is the server side programming language you're using, if any?

Check out the answers here: How to get status code of remote url using Javascript/Ajax but NOT using jQuery?. Same question as this, and the answers cover all the bases.

Community
  • 1
  • 1
Gromer
  • 9,861
  • 4
  • 34
  • 55
  • It was me lol. Removed the comment when I saw the OP is asking with js or jquery. – Savas Vedova Aug 17 '12 at 21:32
  • Still very relevant, as most accepted solutions will be using a server side language so you can actually access another domain. – Gromer Aug 17 '12 at 21:33
1

Ajax on clicking every request, even if it's as HEAD request (which is what you generally SHOULD use to test for a url's existence as that's why that HTTP verb exists in the first place), just strikes me as an incredibly bad idea, even if you could get over the likely insurmountable cross domain issues (unless you involve some sort of server-side check invoked by ajax). Now, I suppose you might be able to save responses at some place so that links aren't rechecked (on both client or server side), but this strikes me still as an incredibly over-complicated solution to a problem that generally doesn't need solving.

JayC
  • 7,053
  • 2
  • 25
  • 41