将var传递给另一个页面

时间:2013-09-17 17:00:38

标签: javascript php jquery

是否可以将totalScore var传递到另一个页面onclick,以便可以在那里显示?例如:点击提交链接,转到yourscore.html并在页面上显示分数

$("#process").click(function() {   
    var totalScore = 0;
    $(".targetKeep").each( function(i, tK) {
       if (typeof($(tK).raty('score')) != "undefined") {
          totalScore += $(tK).raty('score');
       }
    });
    alert("Total Score = "+totalScore);
});

3 个答案:

答案 0 :(得分:2)

我们假设您的HTML可能如下:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

  <script>
    $(document).ready(function(){

      $("#process").click(function() {   
        var totalScore = 0;
        /*
         Your code to calculate Total Score
         Remove the next line in real code.
        */
        totalScore = 55; //Remove this

    alert("Total Score = "+totalScore);
      $("#submit-link").attr('href',"http://example.com/yourscore.html?totalScore="+totalScore);  
});


    });
  </script>

<meta charset=utf-8 />
<title>JS Bin</title>

</head>
<body>
  <button id="process">Process</button>
<br />
<a href="#" id="submit-link">Submit Total Score</a>

</body>
</html>

查看此DEMO

在yourscore.html中,您可以在以下排队中了解更多信息,以便从网址中提取网址参数:

Parse URL with jquery/ javascript?

答案 1 :(得分:1)

这通常是通过更改页面的URL来完成的。即如果你要去一个新的页面,只需:

http://example.com/new/page?param1=test

如果页面已存在于新窗口中(如您拥有的弹出窗口),请将该网址设置为新的:

http://example.com/new/page#param

打开一个窗口:

 var win = window.open('http://example.com/new/page?totalscore'+totalscore,'window');

更改位置:

 win.location.href='http://example.com/new/page?totalscore'+totalscore;

其他方法可以是websockets或HTML5中的cookies或localstorage。

答案 2 :(得分:-1)

如果您的目标是支持更现代的浏览器,优雅的解决方案可能是使用sessionStorage或localStorage!它非常简单,可以根据需要清除和设置。低端的最大大小是2mb,但如果你只存储INT,那么你应该没问题。

DOCS: http://www.html5rocks.com/en/features/storage http://dev.w3.org/html5/webstorage/

样本: http://html5demos.com/storage

实施例

addEvent(document.querySelector('#local'), 'keyup', function () {
  localStorage.setItem('value', this.value);
  localStorage.setItem('timestamp', (new Date()).getTime());
  //GO TO YOUR NEXT PAGEHERE
});
相关问题