我想添加一个按钮/链接,将用户重定向到我的 Blogger 网站上随机选择的帖子。我计划将该按钮作为我的 Blogger 网站顶部页面列表中的链接之一,并让它在每次点击时将用户重定向到随机页面。
我在网站的页脚中添加了一个“HTML/JavaScript”小工具,并尝试从那里解决问题。不幸的是,我所做的一切都还没有使我的这个想法取得成功。我尝试过这样做,以便在加载网站时,链接会获得一个随机选择的 URL,该 URL 会重定向到我网站上的帖子,但是,它只选择最近的帖子,而不选择较旧的帖子。我计划让这个随机链接从我的所有帖子中随机挑选,而不仅仅是较新的帖子。
这是页面列表的代码(我审查的网址):
{
"link0":{"href":"http://###/","position":0,"title":"Home"},
"1442808221004497204":{"href":"https://###","position":1,"title":"About"},
"2724719100104741041":{"href":"https://###","position":2,"title":"Contact"},
"link1":{"href":"javascript:randomPost()","position":3,"title":"Random"}
}
这是 HTML/JavaScript 小工具的代码:
<script>
function randomPost() {
// Get all post URLs on the page
var postLinks = document.querySelectorAll('.post-title a');
// Generate a random index
var randomIndex = Math.floor(Math.random() * postLinks.length);
// Get the URL of the randomly chosen post
var randomPostUrl = postLinks[randomIndex].getAttribute('href');
// Redirect to the random post
window.location.href = randomPostUrl;
}
</script>
你介意把你的问题说清楚一点吗