1

I want to verify if an external url valid/exists/responsive using javascript. For example, "www.google.com" should return true and "www.google123.com" should return false.

I thought to use AJAX for this purpose by testing : if (xmlhttp.readyState == 4 && xmlhttp.status == 200) but it seems that this doesn't work for remote servers(external urls). As my server uses a proxy, i planned to use browser side script so that it automatically uses user's browser proxy if present. Please tell me do I have to use "AJAX Cross Domain"? How to achieve this, as i simply want to validate a url. Any way other than using AJAX?

user972810
  • 11
  • 2

2 Answers2

0

I'm pretty sure this is not possible. Any AJAX that allowed you to call a random page on another domain in the user's context would open up all sorts or security holes.

You will have to use a server-side solution.

alnorth29
  • 3,525
  • 2
  • 34
  • 50
  • As i said my server uses a proxy, so when i create a HttpURLConnection specifying proxy in the URL constructor, it works fine but for https connections, i again faced issue. As HttpsURLConnection con =(HttpsURLConnection) url.openConnection();-doesn't work, works only for HttpURLConnection. So, how to achieve this simple thing? – user972810 Oct 03 '11 at 11:26
  • http://stackoverflow.com/questions/1511674/how-do-a-send-an-https-request-through-a-proxy-in-java might be helpful. It still doesn't sound particularly straightforward. – alnorth29 Oct 05 '11 at 09:47
  • I donot think that the object returned by URL's openConnection() can be type casted to HttpsURLConnection; although it can be casted to HttpURLConnection, HttpsURLConnection connection = (HttpsURLConnection)url.openConnection(); Moreover, i am more interested at browser side validation, so that i donot have to go for these proxy settings. – user972810 Oct 05 '11 at 10:25
0

The usual way to avoid cross-domain issues is to inject a tag. Tags like image or script kan load their content from any domain. You could inject, say a script tag with type "text/x-unknown" or something, and listen to the tags load-event. When the load event triggers, you can remove the script tag from the page again.

Of course, if the files you are looking for happens to be images, then you could new Image() instead. That way you don't have to pollute the page by injecting tags, because images load when they are created (this can be used to preload images). Again, just wait for the load event on the image.

UPDATE

Okay, it seems I am jumping to conclusions here. There is some differences between browsers on how this can be supported. The following is a complete example, of how to use the script tag for validating urls in IE9 and recent versions of Firefox, Chrome and Safari.

It does not work in older versions of IE (IE8 at least) because apparently they don't provide load/error events for script-tags.

Firefox refuses to load anything if the contenttype for the script-tag is not empty or set to 'text/javascript'. This means that it may be somewhat dangerous to use this approach to check for scriptfiles. It seems like the script tag is deleted before any code is executed in my tests, but I don't for sure...

Anyways, here is the code:

<!doctype html>
<html>
<head>
    <script>
        function checkResource(url, callback) {
            var tag = document.createElement('script');
            tag.src = url;
            //tag.type = 'application/x-unknown';
            tag.async = true;
            tag.onload = function (e) {
                document.getElementsByTagName('head')[0].removeChild(tag);
                callback(url, true);
            }
            tag.onerror = function (e) {
                document.getElementsByTagName('head')[0].removeChild(tag);
                callback(url, false);
            }
            document.getElementsByTagName('head')[0].appendChild(tag);
        }
    </script>
</head>
<body>
    <h1>Testing something</h1>
    <p>Here is some text. Something. Something else.</p>
    <script>
        checkResource("http://google.com", function (url, state) { alert(url + ' - ' + state) });
        checkResource("http://www.google.com/this-does-not-exists", function (url, state) { alert(url + ' - ' + state) });
        checkResource("www.asdaweltiukljlkjlkjlkjlwew.com/does-not-exists", function (url, state) { alert(url + ' - ' + state) });
    </script>
</body>
</html>
AHM
  • 5,145
  • 34
  • 37
  • how can i use an image tag for testing a url validity when it is not returning an image. For example, will not work, it will always go to error part, for valid urls too. Any other way? – user972810 Oct 03 '11 at 11:21
  • You can't use an image to test urls that aren't images, but I belive you could use a script tag. Just set the script tags type to "text/plain" or something, so it wont attempt to process it as javascript. In this case you have to insert the tag into the page, and attach a callbackfunction to it's onload event. The callback will then be called if the resource exists. You could also add a callback to onerror, which would then be called if the resource does not exists – AHM Oct 04 '11 at 16:03
  • The script "type" tag takes values like "text/tcl", "text/javascript", "text/vbscript". Now, doesn't work. No alerts. Any other suggestion? – user972810 Oct 05 '11 at 06:02
  • you are right - this is troublesome cross browser. I have added an example. I don't have any solution on hand that is compatible with older versions of IE, but you may be able to do something with the onreadystate event. – AHM Oct 05 '11 at 14:46