基于变量改变页面

时间:2018-04-17 12:02:02

标签: javascript html

我一直在尝试创建一个简单的html页面,它返回一个不同的动画(存储在不同的HTML文档中,虽然这可以改变,我认为它最适合清洁)基于变量" 响应&#34 ;.我一直有这个简单脚本的问题,以返回我想要的结果,我还没有在网上找到许多相关的结果(inb4 LMGTFY)。我知道响应变量是按照需要创建的,因为它打印了所选的数字,现在它似乎是间歇性的,因为它喜欢打印。

有没有人对有效的脚本编写方法提出建议?

干杯, 马特

(当前HTML如下,我没有实现任何CSS或外部脚本)

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">        
    <title>Fielder's Choice Spinner</title>        
    <meta name="description" content="Instore spinner for FC">
    <meta name="author" content="Matthew Davis">        
    <link href="style.css" rel="stylesheet">  
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <!--ill just leave this here-->

    <script language="JavaScript">
        var response = (function getRandomIntInclusive(min, max) {
            min = Math.ceil(1);
            max = Math.floor(1000);
            return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive 
        })();

        document.write(response)

        if (response > 900) {
            window.location.replace("win.html")
        } 
        if (response < 900){
            window.location.replace("lose.html")
        }
    </script>
</head>
<body>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

使用#标签。在Twitter采用它之前,它曾经是程序员的秘密角色。

示例:如果您重定向到&#34; win.html#756&#34;,那么win.html可以使用window.location.hash读取该数字 要删除哈希符号本身,您可以使用.substr(1)

index.html(我改变了一点,带走了你想要的东西)

<script language="JavaScript">
    window.onload = function() {
      var response = function getRandomIntInclusive(min, max) {
        min = Math.ceil(1);
        max = Math.floor(1000);
        return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive 
      };
      var myresponse = response();
      if (myresponse > 900) {
        window.location.replace("win.html#" + myresponse);
      } 
      if (myresponse < 900) {
        window.location.replace("lose.html#" + myresponse);
      }
    }
</script>

win.html(dito for lose.html,消息除外)

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Fielder's Choice Spinner - WIN</title>
<link href="style.css" rel="stylesheet">  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  <script language="JavaScript">
    window.onload = function() {
      var myresponse = window.location.hash.substr(1);
      document.getElementById('data').innerHTML = myresponse;
    }

  </script>
  <style>#data { color: red; font-size: 18px;}</style>
</head>
<body>
  <div id="main">YOU WON --- Your score is: <span id="data"></span></div>
</body>
</html>