Search code examples
javastruts-1

How to check if a URL is accessible from a PC?


We are using struts 1.3 in our application. We have a requirement as stated below -

We have button in login page. Now,

  1. If a url is accessible from the user PC, show the button.
  2. Otherwise don't show it.

To check the URL accessibility, I am using XMLHttpRequest.responseText. Below is my code :

<html>
  <head>
    <title>Test html page</title>
    <script type="text/javascript">
      function checkURL()
      {
        var xmlHttp = null;
        var theUrl = "http://www.googlewetuyyu.co.in";
        xmlHttp = new XMLHttpRequest();
        xmlHttp.open( "GET", theUrl, false );
        alert(xmlHttp.responseText);
      }
    </script>
  </head>
  <body onload="checkURL();">
    <b>Hello World!</b>
  </body>
</html>

But still I am getting nothing in alert box.

[I am modifying a little bit]

It's actually working in IE. But not working in chrome. Please suggest a solution.

Thanks, Kartic


Solution

  • Since cross domain AJAX requests wont work because of the same origin policy, I suggest 2 ways doing it.

    Include a an override CSS file

    <link type="text/css" href="http://website.com/normal.css" />
    <link type="text/css" href="http://otherwebsite.com/override.css" />
    
    <div id="login" class="login">
        <!-- Your button code -->
    </div>
    

    normal.css

    .login
    {
        display: none;
    }
    

    override.css

    #login
    {
        display: block;
    }
    

    If the client has access to the http://otherwebsite.com, the style will be overriden since the CSS file will be loaded and ID is more important than class.

    Image load test

    <img id="img" style="width: 0;height: 0;" src="http://otherdomain.com/smallimage.png" />
    <script>
        $("#img").load(function() { alert("image loaded correctly"); })
        .error(function() { alert("error loading image"); });
    </script>