How do I select and open a random link of a certai

2019-03-04 03:54发布

问题:

I have:

var linksInCategory = document.$('.CategoryTreeLabel').href);
var randomLinkArray = Array(linksInCategory);

I need to go to an external webpage and search that document for any items with the class of .CategoryTreeLabel and get the href attached to it. See below.

randomLinkArray is just an array of the links I queried.

function goThere(link) {
    var the_url = randomLinkArray[Math.floor(Math.random()*randomLinkArray.length)];

    var new_window = window.open(the_url,"new_window","menubar, resizeable. location, toolbar, status, scrollbars");
}

the_url takes value of a randomly selected link out of the array randomLinkArray.

This opens the_url in a new window.

        </script>
    </head>
    <body>
        <form name="the_form">
            <input type="button" name="randomSportsArticle" class="broadGroups" onClick="goThere(this)" src="http://en.wikipedia.org/wiki/Category:Sports" value="Sports"></input>

        </form>
    </body>
<html>

When a user clicks the button, they should be taken to a Wikipedia page that is randomly selected from the array of links from the page we queried (right now set as the src="" in the button).

The things I am not sure about are:

  1. I need to go to an external webpage and search that document for any items with the class of .CategoryTreeLabel and get the href attached to it -- I can't do this in JavaScript because of the Same Origin Policy -- any tricks?

  2. The formula for the random href that the argument of my array takes when its called in function goThere()

  3. Where to put the url "http://en.wikipedia.org/wiki/Category:Sports"--- I want to get the .CategoryTreeLabels from here and create the array which the random URL is selected from

Sorry to go on and on just hoping to be clear. Thanks!