Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Check an external url existence using javascript

    I need to check the whether an external url like http://www.somesite.com exists or not using javascript.

    I have a ajax method for it,

    Code:
    function checkUrl(url) { 
     var xmlhttp=false;
    
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    	try {
    		xmlhttp = new XMLHttpRequest();
    	} catch (e) {
    		xmlhttp=false;
    	}
    }
    if (!xmlhttp && window.createRequest) {
    	try {
    		xmlhttp = window.createRequest();
    	} catch (e) {
    		xmlhttp=false;
    	}
    }
     
     xmlhttp.onreadystatechange=function() {
       
      if (xmlhttp.readyState==4) {
        if (xmlhttp.status==200) {
          alert("URL Exists!");
        }
        else if (xmlhttp.status==404) {
    	alert("URL doesn't exist!");
        }
         else {
           alert("Status is "+xmlhttp.status);
         }
       } 
     }
     xmlhttp.open("HEAD", url ,true);
     xmlhttp.send();
    }
    But is not successful for finding existence of external urls.

    Any help...
    Thanks in advance..

  • #2
    New Coder
    Join Date
    Mar 2009
    Posts
    74
    Thanks
    3
    Thanked 3 Times in 3 Posts
    It's probably not working due to XSS security issues. You could try using a hidden iframe and loading the page in it maybe.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Same Origin Policy

    JavaScript automatically prevents scripts on one server from accessing properties of documents on a different server. This restriction prevents scripts from, for example, fetching private information such as directory structures or user session history

    Simply stated, the SOP states that JavaScript code running on a web page may not interact with any resource not originating from the same web site.

    The SOP limits any AJAX XMLHTTPRequest browser call to URLs on the same server from which the host page was loaded. This means that it is impossible to make any kind of AJAX-style request to a different site than the one from which the page was loaded.



    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •