/    Sign up×
Community /Pin to ProfileBookmark

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.”); } [/code]

to post a comment
JavaScript

17 Comments(s)

Copy linkTweet thisAlerts:
@shawngnauthorJul 24.2013 — 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...
Copy linkTweet thisAlerts:
@ElmurJul 24.2013 — 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();
Copy linkTweet thisAlerts:
@shawngnauthorJul 24.2013 — 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
Copy linkTweet thisAlerts:
@AirbladerJul 24.2013 — 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

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.
Copy linkTweet thisAlerts:
@shawngnauthorJul 24.2013 — 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.
Copy linkTweet thisAlerts:
@ElmurJul 24.2013 — 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];}[/CODE]


Then the score value can be retrieved like this:

[CODE]var score=gup( 'score' );[/CODE]
Copy linkTweet thisAlerts:
@shawngnauthorJul 24.2013 — I'm afraid idont understand the first set of code ?
Copy linkTweet thisAlerts:
@ElmurJul 24.2013 — 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' );
Copy linkTweet thisAlerts:
@shawngnauthorJul 24.2013 — I'm afraid idont understand the first set of code ?
Copy linkTweet thisAlerts:
@shawngnauthorJul 24.2013 — 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.


<i>
</i>&lt;script type="text/JavaScript"&gt;
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&lt;11; QCount++) {
QUsed[QCount] = 1; // Marked as available
}
this.anchor.innerHTML = "";
var HTMLBlob = "&lt;table&gt;";
for (QCount=0; QCount&lt;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 += "&lt;/table&gt;";
this.anchor.innerHTML = HTMLBlob;
}
Questions.prototype.nukeExistingQuiz = function() {
if (null != this.anchor &amp;&amp; null != this.anchor.childCount) {
while (this.anchor.childCount &gt; 0) {
this.anchor.removeChild(this.anchor.childNodes[0]);
}}}
Questions.prototype.AddQuestion = function(QNum, EntryNum) {
var Ix;
var HTMLBlob = "&lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" id="check" + EntryNum + ""
checked="checked"&gt;&lt;/td&gt;&lt;td&gt;&lt;br/&gt;"
+ "&lt;strong&gt;" + this.QText[QNum] + "&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;select id="answer" +
EntryNum + "" size="1"&gt;"
+ "&lt;option selected="selected" value="0"&gt;-- Select an answer --&lt;/option&gt;"
for (Ix=0; Ix&lt;4; Ix++) {
HTMLBlob = HTMLBlob + "&lt;option value="" + Ix + ""&gt;" +
this.QChoice[QNum*4+Ix] + "&lt;/option&gt;";
}
HTMLBlob = HTMLBlob + "&lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;";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&lt;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 &gt; 0) {
alert("You have left " + score + " questions unanswered. Please try again.");
document.getElementById("submit").disabled = false;
score = 0;
}
else {
for (var Ix=0; Ix&lt;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 &lt;= 6 ) {
window.open("torefrences.html");
}}}
Questions.prototype.review = function() {
reviewscore = totalscore
document.location.href = "review.html";
}
function opento() {
document.location.href = "torefrences.html";
}
&lt;/script&gt;
&lt;/head&gt;&lt;body&gt;
&lt;div id="quizblock"&gt;&lt;/div&gt;
&lt;script type="text/JavaScript"&gt;
var MyQuiz = new Questions("quizblock");
MyQuiz.generateQuiz();
&lt;/script&gt;&lt;br&gt;
&lt;button class='clickable' name='submit' onMouseOver='change(this,"btnFocus")'
onMouseOut='change(this,"normBtn")' id='normBtn' onClick="MyQuiz.ScoreIt();"
style="margin-right: 3px"&gt;Submit&lt;/button&gt;
&lt;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"&gt;Return to start&lt;/button&gt;
&lt;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"&gt;Review&lt;/button&gt;
&lt;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"&gt;Retake Quiz&lt;/button&gt;
&lt;button class='clickable' name='invisiButtonb'
onMouseOver='change(this,"btnFocus")' onMouseOut='change(this,"normBtn")'
id='normBtn' onClick="opento();" style="visibility:hidden;"
style="margin-right: 3px"&gt;T.O. Refrences&lt;/button&gt;
&lt;/FORM&gt;
&lt;/body&gt;
&lt;p id="footnote" align="center"&gt;
&lt;/HEAD&gt;&lt;BODY&gt;&lt;/html&gt;
Copy linkTweet thisAlerts:
@ElmurJul 24.2013 — 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;[/CODE]

Then within the button:

[CODE]onClick="location.href=reviewscore;"[/CODE]


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
Copy linkTweet thisAlerts:
@shawngnauthorJul 25.2013 — 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
Copy linkTweet thisAlerts:
@shawngnauthorJul 25.2013 — I think I got it to pass the new url reads name="x". Howrver I am unable to use variable still
Copy linkTweet thisAlerts:
@shawngnauthorJul 25.2013 — 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".


<i>
</i>&lt;html&gt;
&lt;body&gt;
&lt;body bgcolor="#FCFDFA" ondragstart="return false" onselectstart="return false"&gt;
&lt;body onload="gup()"&gt;
&lt;script type="text/JavaScript"&gt;
function gup( reviewscore ){
name = reviewscore(/[[]/,"\[").reviewscore(/[]]/,"\]");
var regexS = "[\?&amp;]"+name+"=([^&amp;#]*)";
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 + "");
}
&lt;/script&gt;
&lt;button class='clickable' name='invisiButtonb' onMouseOver='change(this,"btnFocus")' onMouseOut='change(this,"normBtn")' id='normBtn' onClick="test();" style="visibility:visable;" style="margin-right: 3px"&gt;Test button&lt;/button&gt;
&lt;/body&gt;&lt;/html&gt;
Copy linkTweet thisAlerts:
@shawngnauthorJul 26.2013 — Someone please help googling is not helping with this one
Copy linkTweet thisAlerts:
@shawngnauthorJul 26.2013 — 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
Copy linkTweet thisAlerts:
@shawngnauthorJul 26.2013 — Next question time. How Do i send more then 1 in a url and catch more then one?
×

Success!

Help @shawngn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...