acctman
acctman

Reputation: 4349

Passing a var to another page

Is it possible to pass the totalScore var to another page onclick so that it can be displayed there? ex: click submit link it goes to yourscore.html and display the score on page

$("#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);
});

Upvotes: 1

Views: 147

Answers (3)

SaidbakR
SaidbakR

Reputation: 13552

Let we suppose that your HTML may be as follows:

<!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>

Check out this DEMO

In yourscore.html you may able to know more in the following queation to extract the URL parameter from the URL:

Parse URL with jquery/ javascript?

Upvotes: 2

Jason
Jason

Reputation: 13986

This is generally done by changing the url of the page. i.e. if you are going go to a new page, just do:

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

If the page already exists in a new window (like a popup that you own), set the url to something new:

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

Open a window:

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

Change the location:

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

Other ways of doing this could be websockets or cookies or localstorage in HTML5.

Upvotes: 1

blackeyebeefhorsefly
blackeyebeefhorsefly

Reputation: 1949

if you are aiming to support more modern browsers the elegant solution could be to use sessionStorage or localStorage! Its extremely simple and can be cleared and set as you need it. The maximum size at the low end is 2mb but if your only storing INTs then you should be okay.

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

DEMO: http://html5demos.com/storage

EXAMPLE:

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

Upvotes: -1

Related Questions