[SOLVED] Java script does not work from a web browser
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495
Rep:
Java script does not work from a web browser
Code:
var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); if (computerChoice < 0.34) { computerChoice = "rock"; } else if (computerChoice <= 0.67) { computerChoice = "paper"; } else { computerChoice = "scissors"; } var compare = function(choice1, choice2) { if (choice1 === choice2) { return("The result is a tie!"); } else { if (choice1 === "rock") { if (choice2 === "scissors") { return("rock wins"); } else { return("paper wins"); } } } if (choice1 === "paper") { if (choice2 === "rock") { return("paper wins"); } else { return("scissors wins"); } } if (choice1 === "scissors") { if (choice2 === "paper") { return("scissors wins"); } else { return("rock wins"); } } };
This code works when running through a javascript engine, but it's not working from a webserver. What must I do, so that it will work correctly in a web browser if i added the code to my vps?
1. You prompt a message but the reply is not connected to the function you wrote.
2. The function returns a string but this will not be displayed even if you connect the function to the OK-button. You could use console.log(string) or alert(string) instead of return.
Remember that JavaScript is included in a web-page either in "script" tags or by including a reference to an external library in the HTML page. (Take a look at the source code of any web-page to see what I mean.)
Then, something in the web page must cause that script to be executed in response to some event ... such as the clicking of a mouse button.
There are plentiful examples and tutorials on the Internet already.
Since this is obviously intended as an educational exercise, seek the answer yourself. This is very critical to the learning process. A very important skill that you absolutely must master, is to discover how to frame a problem and to find the answers that you are challenged to produce.
Last edited by sundialsvcs; 04-02-2013 at 05:07 PM.
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495
Original Poster
Rep:
Quote:
Originally Posted by j-ray
1. You prompt a message but the reply is not connected to the function you wrote.
2. The function returns a string but this will not be displayed even if you connect the function to the OK-button. You could use console.log(string) or alert(string) instead of return.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.