簡體   English   中英

如何通過隨機數將網頁重定向到另一個網頁?

[英]How to redirect a webpage to another webpage by random number?

只是想知道如何通過隨機數將一個網頁重定向到另一個網頁。
在HTML中,我會那樣做:

<meta http-equiv="refresh" content="seconds; url=http://example.com/" />

例如,如果最大范圍是1.5,最小范圍是0.5(從0.5到1.5),如何插入它而不是“ seconds”值? 很簡單的問題。
謝謝。

var second = parseInt(Math.random()* (1500 - 500) + 500);
setTimeout(function(){ window.location = "http://example.com/" },second);

更新錯誤的js隨機到php rand這個在http://jsfiddle.net/3SNxg/中測試過的作品

那很容易。

只需將內容設置為空,然后使用jquery:

randomSecond = Math.random(2, 5); // ranges from 2 to 5 seconds

$('#metaID').prop("content", (randomSecond+"; myLinktoThatPage")  );

當然,如果要填充meta標記,可以使用這種方法,但是將window.location.href與setTimeout結合使用,可以幫助您重定向原始JS中的頁面。

但是,如果您不使用jQuery而需要原始Javascript:

randomSecond = Math.random(2, 5); // ranges from 2 to 5 seconds

document.getElementById('MyMetaTagID').setAttribute("content", randomSecond+"; myLinktoThatPage");

您可以嘗試以下方法:

var meta_refresh = document.createElement("meta");
meta_refresh.setAttribute("http-equiv","refresh");
var random_delay = 0.5 + Math.random();
meta_refresh.setAttribute("content",random_delay+"; url=http://example.com/");
document.getElementsByTagName("head")[0].appendChild(meta_refresh);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM