【问题标题】:Load random page without array加载没有数组的随机页面
【发布时间】:2014-04-02 21:42:43
【问题描述】:

我一直在寻找一种方法,当用户按下带有 JavaScript/jQuery 的按钮时将其带到随机页面。我所看到的所有内容都将所有页面放在一个数组中,然后使用脚本随机选择一个索引。

是否可以在不将所有页面添加到数组中的情况下执行此类操作。我不想坐在那里将目录中的每个页面/图像添加到一个大数组中,然后运行脚本,我只想让它通过 appache 目录列表并自己抓取一些东西。

这可以吗?我知道我们应该包含到目前为止的代码,但这更像是一个概念性问题。

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

使用ajax 获取您的链接:

$.ajax({
    method: "GET",
    url: "/getRandomLink", // I don't know which server side language you're using, I presume its PHP
    dataType: "text", // text will be enought if it only returns a link
    success: function(link) {
        window.location.href = link;
    }
});

然后将其放入您选择的事件中。很好很容易。

更新

你的服务返回必须是一个带有链接的字符串。我假设你正在使用 PHP,所以会是这样的:

<?php

    $randomLink = "";// Code to get the random link here

    echo $randomLink; // e.g.: http://www.google.com/

?>

【讨论】:

  • 那么对于你有“/getRandomLink”的地方,我会放什么? @DontVoteMeDown
【解决方案2】:

Something 需要提供文件列表供您的脚本随机选择。这将是一个预生成的数组或一个 ajax 请求。

这些会起作用:

...但出于您的目的,您的服务器端脚本只返回一个随机选择的文件会更有意义,而不是仅将整个列表的带宽用于客户端丢弃除一个之外的所有文件。

【讨论】:

    【解决方案3】:

    编辑以反映 OP 给出的评论。请原谅 jQuery。

    假设你得到的目录索引是一个标准的 Apache 自动索引页面:

    //jQuery doc.ready, because I'm too lazy to do something better ;)
    $(function () {
    
        var links = [];
    
        $.ajax(
            {
                //url: location.pathname, // Use current page
                url: '/directoryname/', //directory index of your choice
                success: saveAjaxedLinks
            }
        );
    
    
        function saveAjaxedLinks (data, e) {
            // Scrape anchors off of page. 
            // Note -- there might be other anchors on the page 
            // that you don't want to pick up, say the "up a directory level" anchors.
            // you can filter those out if you want, but you'll have to determine the criteria for doing so.
            var directory = $('a', data);
    
            //Get anchor nodes and store them.
            links = directory.get();
    
            //Assign event listener to button after the data is ready.
            $('button#yourbuttonid').on( 'click', onclickGetRandomPage );
    
            //uncomment to do action immediately.
            //onclickGetRandomPage();
        }
    
        function onclickGetRandomPage (e) {
            if ( !!e && !!e.preventDefault )  {
                e.preventDefault();
            }
            //if no links on the index, do nothing.
            if ( !links.length ) return;
    
            //Get the a random index from the links.
            var rnd = Math.floor( Math.random()*links.length);
    
            for (var i = 0, len = links.length; i < len; i++ ) {
    
                // Make sure there's an actual href inside the random link. 
                // Also, check to make sure the links arent just hashes. Not necessary if you're just using an apache auto-index.
                // If there are certain types of links you don't want (eg. directory roots), you can filter them out here.
                if ( !!links[rnd].href && links[rnd].href.replace( links[rnd].hash, '' ) !== (location.href.replace( location.hash, '' ) ) ) { 
                    //console.log(links[rnd].href); //Log out the links if you're just testing
                    window.location.href = links[rnd].href;
                    break;
                } else {
                    //That link was no good, get a different one.
                    rnd = Math.floor( Math.random()*links.length );
                }
            }
        }
    
    
    });
    

    【讨论】:

    • 我的原始帖子中可能没有说清楚。例如,您访问 Web 服务器上的一个目录,它有一个目录列表。您选择目录列表中的一个 html 页面,您会看到一个显示“下一步”的按钮。这就是我所拥有的,当您单击下一步时,我希望它将用户带到一个随机页面。该列表位于具有该按钮的页面本身上。
    • 好的,假设您想使用目录列表作为 URL 的来源(从中选择随机链接),您可以使用 ajax 拉入该页面,并从中选择一个随机链接该页面,使用与我以前类似的功能。我将修改我的代码以向您展示我的意思。
    • 我收到一个错误,.on( click, onclickGetRandomPage ); 说“点击未定义”,所以我切换到 .click,现在它可以正常工作了。非常感谢您的帮助和详细的 cmets,代码很容易理解,这是我希望从评论中得到的:真正理解发生了什么。再次感谢!
    • 很高兴为您提供帮助!对 on 声明感到抱歉——“点击”这个词应该用引号引起来。我在示例中清除了那个错字。
    猜你喜欢
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 2015-04-09
    • 1970-01-01
    相关资源
    最近更新 更多