www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: javascript variables inside functions

Hybrid View

  1. #1
    Join Date
    Jul 2013
    Posts
    24

    javascript variables inside functions

    Ok guys I'm trying to pass a JavaScript variable being score in this case from the first function to the second.

    After that I would like to *pass the variable to another html page to display the results of the quiz.
    Any suggestions are appreciated thank you.




    Code:
    Questions.prototype.ScoreIt = function() { *var score = this.AreQuestionsAnswered(); if (score > 0) { alert("You have left " + score + " questions unanswered. Please try again."); document.getElementById("submit").disabled = false; score = 0; } else { for (var Ix=0; Ix<10; Ix++) { if (this.Answer[Ix] == this.thisAns[Ix]) { document.getElementById("check" + Ix).checked = true;
    score++;
    //Ix = Ix+1
    //alert("you got question # " + Ix + " correct"); } else { document.getElementById("check" + Ix).checked = false; } //end else } document.getElementById("submit").disabled = true; var buttonToShow = document.getElementById("invisiButton");
    buttonToShow.style.visibility = "visible"; var buttonToShow = document.getElementById("invisiButtona");
    buttonToShow.style.visibility = "visible"; var buttonToShow = document.getElementById("invisiButtonb");
    buttonToShow.style.visibility = "visible";
    alert("You got " + score + " correct."); alert("Questions with check marks are correct.\n * * *Please click the review button \n * * * * * for more information.");
    }
    }
    //review the results
    Questions.prototype.review = function() { alert ("The number you got right was " + score + " out of 10."); }

  2. #2
    Join Date
    Jul 2013
    Posts
    24
    Ok how do I make code look nicer I had it looking nice and it changed it. Also I got the variable working but not in anothere page...

  3. #3
    Join Date
    Jul 2013
    Posts
    6
    to pass variables between functions, put the variable name inside the brackets after receiving functions name, like this:

    function1() {
    var score = 1;
    }

    function2(score)
    {
    alert("This is from function 2. Score is "score);
    }

    function1();
    function2();

  4. #4
    Join Date
    Jul 2013
    Posts
    24
    Ty man that's a lot easier then what I did. Next question is how do I use that same variable on another linked page

  5. #5
    Join Date
    Jul 2013
    Posts
    66
    The above answer is incorrect, both the syntax (wrong function declaration and you cannot concatenate strings like that) and the usage (function2 is never called with an argument). A correct example would be

    Code:
    function functionA() {
        var someVariable = "some value";
        return someVariable;
    }
    
    function functionB(value) {
        var newValue = "This is the value you passed: " + value;
        return newValue;
    }
    
    var value = functionA();
    alert(functionB(value));
    Passing variables between pages is trickier. One way would be a GET request, which basically means that you provide the variables in the link (e.g. "page.html?someVariable=someValue&anotherVariable=anotherValue). These can be read in Javascript using window.location.search.

  6. #6
    Join Date
    Jul 2013
    Posts
    24
    Iv read aboutthat GET. i have also tried it but can get it to work can you give a example
    Thank you for clearing that up.

  7. #7
    Join Date
    Jul 2013
    Posts
    6
    Pass the score value in a URL parameter like this:

    http://yourdomain.com/folder/?score=2

    then to get the score parameter value, use a function like this:

    Code:
    function gup( name ){
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
    var regexS = "[\\?&]"+name+"=([^&#]*)";  
    var regex = new RegExp( regexS );  
    var results = regex.exec( window.location.href ); 
     if( results == null )    return "";  
    else    return results[1];}
    Then the score value can be retrieved like this:

    Code:
    var score=gup( 'score' );

  8. #8
    Join Date
    Jul 2013
    Posts
    24
    I'm afraid idont understand the first set of code

  9. #9
    Join Date
    Jul 2013
    Posts
    24
    I'm afraid idont understand the first set of code

  10. #10
    Join Date
    Jul 2013
    Posts
    24
    Sry double post I'm on phone lol
    Here is the whole script almost... I want to make the review page display the
    quiz you just took and the answers you chose/what you got wrong and right.
    This is the hardest part for me so far.


    Code:
    <script type="text/JavaScript">
    function Questions(divname) {
    this.QText * = new Array(11); // The questions
    this.QAnswer = new Array(11); // The correct answers
    this.QChoice = new Array(44); // The possible multi-choice values
    this.thisAns = new Array(44); // The answers for this page
    this.Answer *= new Array(10); // The user's answers
    this.anchor = document.getElementById(divname);
    this.QText[0] = "?";
    this.QText[1] = "?";
    this.QText[2] = "?";
    this.QText[3] = "?";
    this.QText[4] = "?";
    this.QText[5] = "?";
    this.QText[6] = "?";
    this.QText[7] = "?";
    this.QText[8] = "?";
    this.QText[9] = "?";
    this.QText[10] = "?";
    this.QChoice[0] = "2";
    this.QChoice[1] = "2";
    this.QChoice[2] = "3";
    this.QChoice[3] = "3";
    this.QChoice[4] = "B";
    this.QChoice[5] = "r";
    this.QChoice[6] = "B";
    this.QChoice[7] = "N";
    this.QChoice[8] = "2";
    this.QChoice[9] = "2";
    this.QChoice[10] = "3";
    this.QChoice[11] = "d";
    this.QChoice[12] = "A";
    this.QChoice[13] = "A";
    this.QChoice[14] = "b";
    this.QChoice[15] = "A";
    this.QChoice[16] = "f";
    this.QChoice[17] = "P";
    this.QChoice[18] = "C";
    this.QChoice[19] = "U";
    this.QChoice[20] = "I";
    this.QChoice[21] = "I";
    this.QChoice[22] = "I";
    this.QChoice[23] = "I";
    this.QChoice[24] = "3";
    this.QChoice[25] = "3";
    this.QChoice[26] = "3";
    this.QChoice[27] = "3";
    this.QChoice[28] = "z";
    this.QChoice[29] = "z";
    this.QChoice[30] = "z";
    this.QChoice[31] = "z";
    this.QChoice[32] = "z";
    this.QChoice[33] = "z";
    this.QChoice[34] = "z";
    this.QChoice[35] = "z";
    this.QChoice[36] = "r";
    this.QChoice[37] = "r";
    this.QChoice[38] = "r";
    this.QChoice[39] = "2";
    this.QChoice[40] = "F";
    this.QChoice[41] = "F";
    this.QChoice[42] = "F";
    this.QChoice[43] = "A";
    this.QAnswer[0] = 1;
    this.QAnswer[1] = 4;
    this.QAnswer[2] = 2;
    this.QAnswer[3] = 2;
    this.QAnswer[4] = 1;
    this.QAnswer[5] = 2;
    this.QAnswer[6] = 3;
    this.QAnswer[7] = 3;
    this.QAnswer[8] = 4;
    this.QAnswer[9] = 4;
    this.QAnswer[10] = 1;
    }
    Questions.prototype.generateQuiz = function() {
    var QCount = 0;
    var QNumber = 0;
    var QUsed = new Array(11);
    for (QCount=0; QCount<11; QCount++) {
    QUsed[QCount] = 1; // Marked as available
    }
    this.anchor.innerHTML = "";
    var HTMLBlob = "<table>";
    for (QCount=0; QCount<10; ) {
    QNumber = Math.floor(11 * Math.random());
    if (1 == QUsed[QNumber]) { // Still available?
    HTMLBlob += this.AddQuestion(QNumber, QCount);
    QCount++;
    QUsed[QNumber] = 0; // Marked as unavailable
    }}
    HTMLBlob += "</table>";
    this.anchor.innerHTML = HTMLBlob;
    }
    Questions.prototype.nukeExistingQuiz = function() {
    if (null != this.anchor && null != this.anchor.childCount) {
    while (this.anchor.childCount > 0) {
    this.anchor.removeChild(this.anchor.childNodes[0]);
    }}}
    Questions.prototype.AddQuestion = function(QNum, EntryNum) {
    var Ix;
    var HTMLBlob = "<tr><td><input type=\"checkbox\" id=\"check" + EntryNum + "\"
    checked=\"checked\"></td><td><br/>"
    + "<strong>" + this.QText[QNum] + "</strong></td><td><select id=\"answer" +
    EntryNum + "\" size=\"1\">"
    + "<option selected=\"selected\" value=\"0\">-- Select an answer --</option>"
    for (Ix=0; Ix<4; Ix++) {
    HTMLBlob = HTMLBlob + "<option value=\"" + Ix + "\">" +
    this.QChoice[QNum*4+Ix] + "</option>";
    }
    HTMLBlob = HTMLBlob + "</select></td></tr>";0
    this.thisAns[EntryNum] = this.QAnswer[QNum]; * // this variable this.thisans
    is the answeres user chose
    return HTMLBlob;
    }
    Questions.prototype.AreQuestionsAnswered = function() {
    var unanswered = 0;
    for (var Ix=0; Ix<10; Ix++) {
    this.Answer[Ix] = document.getElementById("answer" + Ix).selectedIndex;
    if (this.Answer[Ix] == 0) {
    unanswered++;
    }}
    return unanswered;
    }
    Questions.prototype.ScoreIt = function() {
    var score = this.AreQuestionsAnswered();
    if (score > 0) {
    alert("You have left " + score + " questions unanswered. Please try again.");
    document.getElementById("submit").disabled = false;
    score = 0;
    }
    else {
    for (var Ix=0; Ix<10; Ix++) {
    if (this.Answer[Ix] == this.thisAns[Ix]) {
    document.getElementById("check" + Ix).checked = true;
    score++;
    Ix = Ix+1
    alert("you got question # " + Ix + " correct");
    Ix = Ix-1
    totalscore = score
    }
    else {
    document.getElementById("check" + Ix).checked = false;
    totalscore = score
    }}
    document.getElementById("submit").disabled = true;
    var buttonToShow = document.getElementById("invisiButton");
    buttonToShow.style.visibility = "visible";
    var buttonToShow = document.getElementById("invisiButtona");
    buttonToShow.style.visibility = "visible";
    var buttonToShow = document.getElementById("invisiButtonb");
    buttonToShow.style.visibility = "visible";
    alert("You scored a " + score * 10 + "% on this quiz.");
    if (totalscore <= 6 ) {
    window.open("torefrences.html");
    }}}
    Questions.prototype.review = function() {
    reviewscore = totalscore
    document.location.href = "review.html";
    }
    function opento() {
    document.location.href = "torefrences.html";
    }
    </script>
    </head><body>
    <div id="quizblock"></div>
    <script type="text/JavaScript">
    var MyQuiz = new Questions("quizblock");
    MyQuiz.generateQuiz();
    </script><br>
    <button class='clickable' name='submit' onMouseOver='change(this,"btnFocus")'
    onMouseOut='change(this,"normBtn")' id='normBtn' onClick="MyQuiz.ScoreIt();"
    style="margin-right: 3px">Submit</button>
    <button class='clickable' name='Test' onMouseOver='change(this,"btnFocus")'
    onMouseOut='change(this,"normBtn")' id='normBtn' onClick="location.href='test
    start page.html';" style="margin-right: 3px">Return to start</button>
    <button class='clickable' name='invisiButton'
    onMouseOver='change(this,"btnFocus")' onMouseOut='change(this,"normBtn")'
    id='normBtn' onClick="MyQuiz.review();" style="visibility:hidden;"
    style="margin-right: 3px">Review</button>
    <button class='clickable' name='invisiButtona'
    onMouseOver='change(this,"btnFocus")' onMouseOut='change(this,"normBtn")'
    id='normBtn' onClick="document.location.reload(true);"
    style="visibility:hidden;" style="margin-right: 3px">Retake Quiz</button>
    <button class='clickable' name='invisiButtonb'
    onMouseOver='change(this,"btnFocus")' onMouseOut='change(this,"normBtn")'
    id='normBtn' onClick="opento();" style="visibility:hidden;"
    style="margin-right: 3px">T.O. Refrences</button>
    </FORM>
    </body>
    <p id="footnote" align="center">
    </HEAD><BODY></html>

  11. #11
    Join Date
    Jul 2013
    Posts
    6
    It is a function using regex to get parameters from a URL

    If your URL had the parameter

    http://yourdomain.com/?myparam=thisisit

    to get the value of myparam you would use gup( 'myparam' );

  12. #12
    Join Date
    Jul 2013
    Posts
    6
    you want the location href to have have the variable added the end of the url in a parameter like this:

    Code:
    var reviewscore = "review.html?score="+score;
    Then within the button:

    Code:
    onClick="location.href=reviewscore;"

    if the score variable is 4, this will open page: review.html?score=4

    then get the score variable from the review page as i described above in #7

  13. #13
    Join Date
    Jul 2013
    Posts
    24
    Maybe I dornt know javascript well enough but this doesn't make sence nor does it work. Maybe I'm doing it wrong : but ty for responding so quick

  14. #14
    Join Date
    Jul 2013
    Posts
    24
    Here is code for the review.html with a lot of the css and crap cut out.
    What is wrong with it why is reviewscore undefined. *That is the variable I am passing. It passes but I cant "get".


    Code:
    <html>
    <body>
    <body bgcolor="#FCFDFA" ondragstart="return false" onselectstart="return false">
    <body onload="gup()">
    <script type="text/JavaScript">
    function gup( reviewscore ){
    name = reviewscore(/[\[]/,"\\\[").reviewscore(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    *if( results == null ) * *return "";
    else * *return results[1];}
    var score=gup( 'reviewscore' );
    function test() {
    alert("testing" + reviewscore + "");
    }
    </script>
    <button class='clickable' name='invisiButtonb' onMouseOver='change(this,"btnFocus")' onMouseOut='change(this,"normBtn")' id='normBtn' onClick="test();" style="visibility:visable;" style="margin-right: 3px">Test button</button>
    </body></html>

  15. #15
    Join Date
    Jul 2013
    Posts
    24
    Problem solved I'm retarded had I slowed down and read I would have relise your code was exactly what I needed I did not need to change names... thank you for your help good sir

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles