开发者

URL validation/connectivity using javascript

开发者 https://www.devze.com 2023-04-09 06:59 出处:网络
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 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 th开发者_StackOverflowought 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?


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.


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>
0

上一篇:

:下一篇

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号