Welcome, guest | Sign In | My Account | Store | Cart

Save this code as randomURL.html. Everytime you open the file w/ a browser it will try to visit a random Internet page. Unfortunately most random trials does not take you a browser renderable webpage. I am open to ideas on how to make certain it finds a good webpage everytime.

JavaScript, 49 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<script type="text/javascript">
function checkUrl(theURL) 
{
    var request = false;
    //Firefox and Netscape
    if (window.XMLHttpRequest) 
    {
        request = new XMLHttpRequest;
    }
    //IE
    else if (window.ActiveXObject) 
    {
        request = new ActiveXObject("Microsoft.XMLHttp");
    }
    
    if (request) 
    {
        request.open("GET", theURL);
        request.onreadystatechange = function() 
        {
            if (request.status == 200 && request.readyState == 4)
            {
                //page is there
                return true;
            }
            else if (request.status == 404) 
            {
                //page is not there
                return false;
            }
        }
        request.send(null);
    }
}

do
{
    //ping www.google.com => 64.233.167.147
    var ip0=Math.floor(Math.random()*256);
    var ip1=Math.floor(Math.random()*256);
    var ip2=Math.floor(Math.random()*256);
    var ip3=Math.floor(Math.random()*256);

    theURL="http://"+ip0+"."+ip1+"."+ip2+"."+ip3+"/";
}
while(checkUrl(theURL)==false);

window.location=theURL;
</script>

1 comment

Joe 11 years, 3 months ago  # | flag
Created by FB36 on Thu, 25 Mar 2010 (MIT)
JavaScript recipes (69)
FB36's recipes (148)

Required Modules

  • (none specified)

Other Information and Tasks