How to link a javascrit page to a tab I created, JAVASCRIPT TO HTML |
How to link a javascrit page to a tab I created, JAVASCRIPT TO HTML |
bleedz129 |
Mar 24 2014, 11:17 AM
Post
#1
|
Novice Group: Members Posts: 21 Joined: 24-March 14 Member No.: 20,600 |
Hey guys I'm new here
Now I am coding on notepad++ this is my HTML code CODE <!DOCTYPE HTML> <link rel="stylesheet" type="text/css" href="cssmain.css" /> <head></head> <body> <h1 id="img1"><img src="portfoliopic.jpg" /></h1> <ul class="tabs"> <li><a href="Aboutme.html">aboutme</a></li> <li><a>rpsgame</a></li> <li><a href="#">Tab 3</a></li> </ul> </body> Now what I am trying to do, is link my javascript program, which is on another page of notepad++ to the rpsgame tab on the list (the second one) how do I do that ? this is my javascript page code CODE var choice; var compChoice; var x = 0; //start console.log("Welcome!"); choice = prompt("You are going against my computer, he is as smart as Watson is ! what is your choice ? "); //in case you entered something wrong if(Choice != "Rock" || Choice != "Scissors" || Choice != "Paper" ) { while(x==0) { choice = prompt("Invalid, Please enter one of these choices: Rock, Paper, Scissors "); if(Choice != "Rock" || Choice != "Scissors" || Choice != "Paper" ) x==0; else x==1; } } //computer compChoice = Math.floor((Math.random()*3)+1); if(compChoice === 1) compChoice = "Paper"; else if (compChoice === 2) compChoice = "Rock"; else compChoice = "Scissors"; //comparing if(choice === "Rock") { if(compChoice === "Scissors") console.log("Computer Choice:" + " " + compChoice); console.log("you Won! "); else console.log("Computer Choice:" + " " + compChoice); console.log("you lose :( "); } if(choice === "paper") { if(compChoice === "Scissors") console.log("Computer Choice:" + " " + compChoice); console.log("you lose :( "); else console.log("Computer Choice:" + " " + compChoice); console.log("you won!"); } if(choice === "Scissors") { if(compChoice === "Rock") console.log("Computer Choice:" + " " + compChoice); console.log("you lose :( "); else console.log("Computer Choice:" + " " + compChoice); console.log("you won!"); } THANKS A BUNCH |
pandy |
Mar 24 2014, 01:24 PM
Post
#2
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,730 Joined: 9-August 06 Member No.: 6 |
If I understand you right, you can't do that, not the way you think.
You link to the JS-file in head with the SCRIPT tag. Then you call it when the tab is clicked (as I guess is when you want it to fire). To stop the script from running already when the page loads you need to put it in a function and that function is what you call onclick. |
bleedz129 |
Mar 24 2014, 02:35 PM
Post
#3
|
Novice Group: Members Posts: 21 Joined: 24-March 14 Member No.: 20,600 |
If I understand you right, you can't do that, not the way you think. You link to the JS-file in head with the SCRIPT tag. Then you call it when the tab is clicked (as I guess is when you want it to fire). To stop the script from running already when the page loads you need to put it in a function and that function is what you call onclick. okay that sounds good. so <script src="rpsgame.js"></script> is added in the html file. How do you call it when the tab is clicked ? do you want me to put the script in a function ? |
pandy |
Mar 24 2014, 02:55 PM
Post
#4
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,730 Joined: 9-August 06 Member No.: 6 |
Yes, put it in a function.
CODE function blahBlah() { //script here } Then in the link you add a function call. CODE onclick="blahBlah()" The script doesn't look right though. Does it run? |
bleedz129 |
Mar 24 2014, 07:07 PM
Post
#5
|
Novice Group: Members Posts: 21 Joined: 24-March 14 Member No.: 20,600 |
Yes, put it in a function. CODE function blahBlah() { //script here } Then in the link you add a function call. CODE onclick="blahBlah()" The script doesn't look right though. Does it run? It doesn't run . I know I am doing something wrong. I can't use the function anyhow in an html file, but you said we should add the script in an html file that is what's confusing me CODE <!DOCTYPE HTML> <link rel="stylesheet" type="text/css" href="cssmain.css" /> <head></head> <body> function rps() { <script src="rpsgame.js"></script> } <h1 id="img1"><img src="portfoliopic.jpg" /></h1> <ul class="tabs"> <li><a href="Aboutme.html">aboutme</a></li> <li><a href="rpsgame.js" onclick="rps();">rpsgame</a></li> <li><a href="#">Tab 3</a></li> </ul> </body> |
CharlesEF |
Mar 24 2014, 08:59 PM
Post
#6
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Your <link> and <script> tags are in the wrong place. Also, your <a> should be changed. Try this:
CODE <!DOCTYPE HTML> <head> <link rel="stylesheet" type="text/css" href="cssmain.css" /> <script src="rpsgame.js"></script> </head> <body> <h1 id="img1"><img src="portfoliopic.jpg" /></h1> <ul class="tabs"> <li><a href="Aboutme.html">aboutme</a></li> <li><a href="#" onclick="rps();">rpsgame</a></li> <li><a href="#">Tab 3</a></li> </ul> </body> One more thing, the rps() function is wrong. You need to make the entire script a function called rps(). Just wrap the entire script (rpsgame.js) in the function makers. |
pandy |
Mar 25 2014, 05:40 AM
Post
#7
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,730 Joined: 9-August 06 Member No.: 6 |
It doesn't run . I'm not familiar with console.log . What is that? Then, the if else statements. You need to have them within curly braces if they contain more than one line. QUOTE I know I am doing something wrong. I can't use the function anyhow in an html file, but you said we should add the script in an html file that is what's confusing me No, not a html file. A js file. But it looks like you got that right, so I'm not sure what you mean. |
CharlesEF |
Mar 25 2014, 08:23 AM
Post
#8
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
console.log is a feature of Firebug and used mainly for debugging. I did not notice them at first. The script might have to be changed.
This post has been edited by CharlesEF: Mar 25 2014, 08:44 AM |
bleedz129 |
Mar 25 2014, 09:25 AM
Post
#9
|
Novice Group: Members Posts: 21 Joined: 24-March 14 Member No.: 20,600 |
One more thing, the rps() function is wrong. You need to make the entire script a function called rps(). Just wrap the entire script (rpsgame.js) in the function makers. Okay this is what I did I have an HTML file called portfolio.HTML with this code CODE [code]<!DOCTYPE HTML> <head> <link rel="stylesheet" type="text/css" href="cssmain.css" /> <script src="rpsgame.js"></script> </head> <body> <h1 id="img1"><img src="portfoliopic.jpg" /></h1> <ul class="tabs"> <li><a href="Aboutme.html">aboutme</a></li> <li><a href="rpsgame.js" onclick="rps();">rpsgame</a></li> <li><a href="#">Tab 3</a></li> </ul> </body> Now I have a javascript file named rpsgame.js with this code CODE //paper, Rock, Scissor function rps() { var choice; var compChoice; var x = 0; //start console.log("Welcome!"); choice = prompt("You are going against my computer, he is as smart as Watson is ! what is your choice ? "); //in case you entered something wrong if(Choice != "Rock" || Choice != "Scissors" || Choice != "Paper" ) { while(x==0) { choice = prompt("Invalid, Please enter one of these choices: Rock, Paper, Scissors "); if(Choice != "Rock" || Choice != "Scissors" || Choice != "Paper" ) x==0; else x==1; } } //computer compChoice = Math.floor((Math.random()*3)+1); if(compChoice === 1) compChoice = "Paper"; else if (compChoice === 2) compChoice = "Rock"; else compChoice = "Scissors"; //comparing if(choice === "Rock") { if(compChoice === "Scissors") { console.log("Computer Choice:" + " " + compChoice); console.log("you Won! "); } else { console.log("Computer Choice:" + " " + compChoice); console.log("you lose :( ");} } if(choice === "paper") { if(compChoice === "Scissors") { console.log("Computer Choice:" + " " + compChoice); console.log("you lose :( "); } else { console.log("Computer Choice:" + " " + compChoice); console.log("you won!");} } if(choice === "Scissors") { if(compChoice === "Rock") { console.log("Computer Choice:" + " " + compChoice); console.log("you lose :( ");} else { console.log("Computer Choice:" + " " + compChoice); console.log("you won!");} } } What is the problem with this code ? when I click the second <li> tab which is rpsgame, it opens the javascript page... in code ! not the actual program. I do not think the problem is the console.log, since if i put the program in between the script of the html file it works. but what I want is to actually link it from an individul html file. thank you guys for helping me out. once i figure this out everything else would work |
CharlesEF |
Mar 25 2014, 09:35 AM
Post
#10
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
The problem is that your javascript contains many syntax errors. I have changed the javascript file so that it does run. I think there is some logic errors but I did not address them.
CODE function rps() { var choice; var compChoice; var x = 0; //start alert("Welcome!"); choice = prompt("You are going against my computer, he is as smart as Watson is! What is your choice?"); if(choice != "Rock" && choice != "Scissors" && choice != "Paper") { while(choice != "Rock" && choice != "Scissors" && choice != "Paper") { choice = prompt("Invalid, Please enter one of these choices: Rock, Paper, Scissors "); } } //computer compChoice = Math.floor((Math.random()*3)+1); if(compChoice === 1) { compChoice = "Paper"; } else if(compChoice === 2) { compChoice = "Rock"; } else { compChoice = "Scissors"; } //comparing if(choice === "Rock") { if(compChoice === "Scissors") { alert("Computer Choice: " + " " + compChoice); alert("you Won!"); } else { alert("Computer Choice: " + " " + compChoice); alert("you lose :( "); } } if(choice === "Paper") { if(compChoice === "Scissors") { alert("Computer Choice: " + " " + compChoice); alert("you Won!"); } else { alert("Computer Choice: " + " " + compChoice); alert("you lose :( "); } } if(choice === "Scissors") { if(compChoice === "Rock") { alert("Computer Choice: " + " " + compChoice); alert("you Won!"); } else { alert("Computer Choice: " + " " + compChoice); alert("you lose :( "); } } } |
bleedz129 |
Mar 25 2014, 09:47 AM
Post
#11
|
Novice Group: Members Posts: 21 Joined: 24-March 14 Member No.: 20,600 |
The problem is that your javascript contains many syntax errors. I have changed the javascript file so that it does run. I think there is some logic errors but I did not address them. CODE function rps() { var choice; var compChoice; var x = 0; //start alert("Welcome!"); choice = prompt("You are going against my computer, he is as smart as Watson is! What is your choice?"); if(choice != "Rock" && choice != "Scissors" && choice != "Paper") { while(choice != "Rock" && choice != "Scissors" && choice != "Paper") { choice = prompt("Invalid, Please enter one of these choices: Rock, Paper, Scissors "); } } //computer compChoice = Math.floor((Math.random()*3)+1); if(compChoice === 1) { compChoice = "Paper"; } else if(compChoice === 2) { compChoice = "Rock"; } else { compChoice = "Scissors"; } //comparing if(choice === "Rock") { if(compChoice === "Scissors") { alert("Computer Choice: " + " " + compChoice); alert("you Won!"); } else { alert("Computer Choice: " + " " + compChoice); alert("you lose :( "); } } if(choice === "Paper") { if(compChoice === "Scissors") { alert("Computer Choice: " + " " + compChoice); alert("you Won!"); } else { alert("Computer Choice: " + " " + compChoice); alert("you lose :( "); } } if(choice === "Scissors") { if(compChoice === "Rock") { alert("Computer Choice: " + " " + compChoice); alert("you Won!"); } else { alert("Computer Choice: " + " " + compChoice); alert("you lose :( "); } } } Your awesome !! it's working. I see that you changed the console.log to alert button. was that part of why it wasn't working ? Also why when the program when is done it opens in javascript code at the end, can i prevent that from happening ? This post has been edited by bleedz129: Mar 25 2014, 09:47 AM |
CharlesEF |
Mar 25 2014, 10:12 AM
Post
#12
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
console.log is used in Firebug. You would need Firebug open showing the console in order to see the output. However, with all the syntax errors I'm surprised it ever ran.
As for your problem at the end. I do not have that problem. After I click on the alert boxes I'm left on the same page. I can click the 2nd link again and run the program all over. I can do this over and over and the javascript code never shows up anywhere. This is my html code: CODE <!DOCTYPE HTML> <head> <link rel="stylesheet" type="text/css" href="cssmain.css" /> <script src="rpsgame.js"></script> </head> <body> <h1 id="img1"><img src="portfoliopic.jpg" /></h1> <ul class="tabs"> <li><a href="Aboutme.html">aboutme</a></li> <li><a href="#" onclick="rps();">rpsgame</a></li> <li><a href="#">Tab 3</a></li> </ul> </body> As for the logic error I was talking about, paper covers rock but according to this program I lose. This post has been edited by CharlesEF: Mar 25 2014, 10:14 AM |
CharlesEF |
Mar 25 2014, 12:37 PM
Post
#13
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Here is a modified version of your code. The output goes to a <p> section on the page.
rpsgame.js CODE function rps(dest) { var choice, compChoice; //start dest.innerHTML = "Welcome!<br>"; choice = prompt("You are going against my computer, he is as smart as Watson is! What is your choice?"); if(choice != "Rock" && choice != "Scissors" && choice != "Paper") { while(choice != "Rock" && choice != "Scissors" && choice != "Paper") { choice = prompt("Invalid, Please enter one of these choices: Rock, Paper, Scissors "); } } //computer compChoice = Math.floor((Math.random()*3)+1); if(compChoice === 1) { compChoice = "Paper"; } else if(compChoice === 2) { compChoice = "Rock"; } else { compChoice = "Scissors"; } //comparing if(choice === "Rock") { if(compChoice === "Scissors") { dest.innerHTML += "Your Choice: " + choice + "<br>"; dest.innerHTML += "Computer Choice: " + compChoice + "<br>"; dest.innerHTML += "you Won!<br>"; } else { dest.innerHTML += "Your Choice: " + choice + "<br>"; dest.innerHTML += "Computer Choice: " + compChoice + "<br>"; dest.innerHTML += "you lose :( <br>"; } } if(choice === "Paper") { if(compChoice === "Scissors") { dest.innerHTML += "Your Choice: " + choice + "<br>"; dest.innerHTML += "Computer Choice: " + compChoice + "<br>"; dest.innerHTML += "you Won!<br>"; } else { dest.innerHTML += "Your Choice: " + choice + "<br>"; dest.innerHTML += "Computer Choice: " + compChoice + "<br>"; dest.innerHTML += "you lose :( <br>"; } } if(choice === "Scissors") { if(compChoice === "Rock") { dest.innerHTML += "Your Choice: " + choice + "<br>"; dest.innerHTML += "Computer Choice: " + compChoice + "<br>"; dest.innerHTML += "you Won!<br>"; } else { dest.innerHTML += "Your Choice: " + choice + "<br>"; dest.innerHTML += "Computer Choice: " + compChoice + "<br>"; dest.innerHTML += "you lose :( <br>"; } } } The HTML is: CODE <!DOCTYPE HTML> <head> <link rel="stylesheet" type="text/css" href="cssmain.css" /> <script src="rpsgame.js"></script> </head> <body> <h1 id="img1"><img src="portfoliopic.jpg" /></h1> <ul class="tabs"> <li><a href="Aboutme.html">aboutme</a></li> <li><a href="#" onclick="rps(document.getElementById('game'));">rpsgame</a></li> <li><a href="#">Tab 3</a></li> </ul> <p id="game"></p> </body> This post has been edited by CharlesEF: Mar 25 2014, 12:39 PM |
CharlesEF |
Mar 25 2014, 12:46 PM
Post
#14
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
You can condense the comparing part of the code as such:
CODE //comparing if(choice === "Rock") { if(compChoice === "Scissors") { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compChoice + "<br>you Won!<br>"; } else { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compChoice + "<br>you lose :( <br>"; } } if(choice === "Paper") { if(compChoice === "Scissors") { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compChoice + "<br>you Won!<br>"; } else { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compChoice + "<br>you lose :( <br>"; } } if(choice === "Scissors") { if(compChoice === "Rock") { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compChoice + "<br>you Won!<br>"; } else { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compChoice + "<br>you lose :( <br>"; } } I did not mess with the logic errors. |
bleedz129 |
Mar 25 2014, 04:00 PM
Post
#15
|
Novice Group: Members Posts: 21 Joined: 24-March 14 Member No.: 20,600 |
QUOTE console.log is used in Firebug. You would need Firebug open showing the console in order to see the output. However, with all the syntax errors I'm surprised it ever ran. As for your problem at the end. I do not have that problem. After I click on the alert boxes I'm left on the same page. I can click the 2nd link again and run the program all over. I can do this over and over and the javascript code never shows up anywhere. got ya I had the href = "rpsgame.js" so after the javascript code runs it actually open the code. so it's working now pretty much regarding my logical errors I fixed them but i was wondering if i can do math random between 1 and 3 like I did, but excluding 2... that way I can make sure there is no draw, instead of adding an additional else if CODE //paper, Rock, Scissor function rps() { var choice; var compChoice; var x = 0; //start alert("Welcome!"); choice = prompt("You are going against my computer, he is as smart as Watson is! What is your choice?").toLowerCase(); if(choice != "rock" && choice != "scissors" && choice != "paper") { while(choice != "rock" && choice != "scissors" && choice != "paper") { choice = prompt("Invalid, Please enter one of these choices: rock, paper, scissors "); } } //computer compChoice = Math.floor((Math.random()*3)+1); if(compChoice === 1) { compChoice = "paper"; } else if(compChoice === 2) { compChoice = "rock"; } else { compChoice = "scissors"; } //comparing if(choice === "rock") { if(compChoice === "scissors") { alert("Computer Choice: " + " " + compChoice); alert("you Won!"); } else { alert("Computer Choice: " + " " + compChoice); alert("you lose :( "); } } if(choice === "paper") { if(compChoice === "rock") { alert("Computer Choice: " + " " + compChoice); alert("you Won!"); } else { alert("Computer Choice: " + " " + compChoice); alert("you lose :( "); } } if(choice === "scissors") { if(compChoice === "paper") { alert("Computer Choice: " + " " + compChoice); alert("you Won!"); } else { alert("Computer Choice: " + " " + compChoice); alert("you lose :( "); } } } |
bleedz129 |
Mar 25 2014, 04:17 PM
Post
#16
|
Novice Group: Members Posts: 21 Joined: 24-March 14 Member No.: 20,600 |
Never mind that wouldn't work... How can i prevent compChoice of being equal to choice ? I don't want to add another else if statement if they are equal and ask user to enter again.
|
CharlesEF |
Mar 25 2014, 05:15 PM
Post
#17
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
You would have to change the logic of the script. compChoice is a number and choice is a string. You can't compare the two. You can setup an array and compare it that way. Here is the rpsgame.js file with those changes in mind.
CODE function rps(dest) { var choice, compChoice; var compArray = new Array("Paper", "Rock", "Scissors"); //start dest.innerHTML = "Welcome!<br>"; choice = prompt("You are going against my computer, he is as smart as Watson is! What is your choice?"); if(choice != "Rock" && choice != "Scissors" && choice != "Paper") { while(choice != "Rock" && choice != "Scissors" && choice != "Paper") { choice = prompt("Invalid, Please enter one of these choices: Rock, Paper, Scissors "); } } //computer compChoice = Math.floor((Math.random()*3)); while(compArray[compChoice] == choice && compChoice < compArray.length) { compChoice = Math.floor((Math.random()*3)); } //comparing if(choice === "Rock") { if(compArray[compChoice] === "Scissors") { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compArray[compChoice] + "<br>you Won!<br>"; } else { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compArray[compChoice] + "<br>you lose :( <br>"; } } if(choice === "Paper") { if(compArray[compChoice]=== "Scissors") { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compArray[compChoice] + "<br>you Won!<br>"; } else { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compArray[compChoice] + "<br>you lose :( <br>"; } } if(choice === "Scissors") { if(compArray[compChoice]=== "Rock") { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compArray[compChoice] + "<br>you Won!<br>"; } else { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compArray[compChoice] + "<br>you lose :( <br>"; } } } Now, you still might need to work on it but this should get you started. I edited this to make some changes. I posted an older version. This post has been edited by CharlesEF: Mar 25 2014, 05:20 PM |
bleedz129 |
Mar 25 2014, 05:45 PM
Post
#18
|
Novice Group: Members Posts: 21 Joined: 24-March 14 Member No.: 20,600 |
You would have to change the logic of the script. compChoice is a number and choice is a string. You can't compare the two. You can setup an array and compare it that way. Here is the rpsgame.js file with those changes in mind. CODE function rps(dest) { var choice, compChoice; var compArray = new Array("Paper", "Rock", "Scissors"); //start dest.innerHTML = "Welcome!<br>"; choice = prompt("You are going against my computer, he is as smart as Watson is! What is your choice?"); if(choice != "Rock" && choice != "Scissors" && choice != "Paper") { while(choice != "Rock" && choice != "Scissors" && choice != "Paper") { choice = prompt("Invalid, Please enter one of these choices: Rock, Paper, Scissors "); } } //computer compChoice = Math.floor((Math.random()*3)); while(compArray[compChoice] == choice && compChoice < compArray.length) { compChoice = Math.floor((Math.random()*3)); } //comparing if(choice === "Rock") { if(compArray[compChoice] === "Scissors") { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compArray[compChoice] + "<br>you Won!<br>"; } else { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compArray[compChoice] + "<br>you lose :( <br>"; } } if(choice === "Paper") { if(compArray[compChoice]=== "Scissors") { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compArray[compChoice] + "<br>you Won!<br>"; } else { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compArray[compChoice] + "<br>you lose :( <br>"; } } if(choice === "Scissors") { if(compArray[compChoice]=== "Rock") { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compArray[compChoice] + "<br>you Won!<br>"; } else { dest.innerHTML += "Your Choice: " + choice + "<br>Computer Choice: " + compArray[compChoice] + "<br>you lose :( <br>"; } } } Now, you still might need to work on it but this should get you started. I edited this to make some changes. I posted an older version. Alright I will. Thank you for all your help buddy |
Lo-Fi Version | Time is now: 23rd April 2024 - 11:06 PM |