2016-12-25 146 views
0

如何將randString的值傳遞給URLURL = randString不起作用。如何通過html獲得id值

<!DOCTYPE html> 
<html> 
<body> 
<h1 id ="test">s1</h1> 
<script> 
    var str1 = "http://www.vofoo.ru/reklam1.html"; 
    var str2 = "http://www.viewtubes.ru/reklam1.html"; 
    var str3 = "http://nofnof.xyz/reklam1.html"; 
    var array = [str1, str2, str3]; 
    var randString = array[Math.floor(Math.random()*array.length)]; 
    document.getElementById("test").innerHTML = randString; 
</script> 
<META HTTP-EQUIV=REFRESH CONTENT="1; URL=document.getElementById(test).value">  
</body> 
</html> 

關閉腳本標籤URL無法識別randString

回答

2

後你爲什麼使用元時,你可以在JS本身做到這一點?

有這樣的說法:

window.location.href = randString; 

像這樣:

var str1 = "http://www.vofoo.ru/reklam1.html"; 
var str2 = "http://www.viewtubes.ru/reklam1.html"; 
var str3 = "http://nofnof.xyz/reklam1.html"; 

var array = [str1, str2, str3]; 
var randString = array[Math.floor(Math.random()*array.length)]; 
document.getElementById("test").innerHTML = randString; 
window.location.href = randString; 
+1

作品!謝謝 – user3422485

0

您可以使用javascript將屬性設置爲meta標籤。動態。

但請確保meta標籤必須在head部分html

我不知道你爲什麼想嘗試metarefresh。你可以用簡單的javascript來做到這一點。

希望這個片段可以幫助您與meta刷新

var str1 = "http://www.vofoo.ru/reklam1.html"; 
 
var str2 = "http://www.viewtubes.ru/reklam1.html"; 
 
var str3 = "http://nofnof.xyz/reklam1.html"; 
 

 
var array = [str1, str2, str3]; 
 
var randString = array[Math.floor(Math.random()*array.length)]; 
 
document.getElementById("test").innerHTML = randString; 
 
var metaTag = document.getElementById("meta-refresh"); 
 
metaTag.setAttribute("content", "1;URL=" + randString); 
 

 
console.log(metaTag.getAttribute("content")); //just to check URL is attached to meta tag.
<!DOCTYPE html> 
 
<html> 
 
<body> 
 
<h1 id ="test">s1</h1> 
 

 
<script> 
 

 

 
</script> 
 
<META HTTP-EQUIV=REFRESH id="meta-refresh"> 
 

 
</body> 
 
</html>

+0

metaTag.setAttribute(「content」,「1; URL =」+ randString) – user3422485

+0

有一個異常null – user3422485

+0

它究竟在哪裏? –

0

它看起來像你的目標是使用meta標記來重定向。我已經將刷新停止在點擊h1之後的60秒和10秒。順便說一句,元總是屬於頭。

代碼片段

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <META HTTP-EQUIV=REFRESH CONTENT="60;" /> 
 
</head> 
 

 
<body> 
 

 
    <h1 id="test">s1</h1> 
 
    <p>Click title<sup>▲▲▲</sup>and wait 10 seconds</p> 
 
    <script> 
 
    //You can use window.location to the same effect 
 
    var here = window.location; 
 
    var str1 = "http://www.msn.com"; 
 
    var str2 = "http://www.w3schools.com"; 
 
    var str3 = "http://bing.com"; 
 
    var test = document.getElementById('test'); 
 
    var meta = document.querySelector('meta'); 
 
    var array = [str1, str2, str3]; 
 
    var randString = array[Math.floor(Math.random() * array.length)]; 
 
    test.textContent = randString; 
 
    test.onclick = function() { 
 
     meta.setAttribute("content", "10; " + randString); 
 
    } 
 
    </script> 
 

 

 
</body> 
 

 
</html>