TheNumnut TheNumnut - 15 days ago 5
HTML Question

When I add a certain line to my Javascript code it removes the onclick function I have for my HTML function.

I am coding a game and I need to save some variables with cookies. I added a line that should execute a Javascript function that will check if the user has already been on the website and made an account or not, and if they have not it will ask for a username. However, when I added the line that should run the function, the function after it failed, and so did it.
This is a portion of my code:

<script>
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function checkCookie() {
var username=getCookie("username");
if (username!="") {
alert("Welcome again " + username);
} else {
username = prompt("Please enter your username:", "");
if (username != "" && username != null) {
setCookie("username", username, 365);
}
}
}
</script>

<button id="open_market">Market</button>
<script>
checkCookie();
open_market.onclick = function() {
window.open("https://thenumnut.github.io/Wars-of-Shares/Market");
}
</script>


The line that I added to run the function was "checkCookie();". After I added that line the button with id "open_market" would not respond when clicked. This makes me think that the code failed/crashed on the line before it when I said "checkCookie();".
If anybody can find the problem with this code I will greatly appreciate it. You can see in the code the github link to my code on github if you would like to see my full code.

Answer

I added your getCookie() function, and apart from that, just added a bit of HTML around the tags, and here's the code:

<!DOCTYPE html> <html> <head> <script> function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length,c.length);
        }
    }
    return ""; }

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+ d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; } function checkCookie() {
    var username=getCookie("username");
    if (username!="") {
        alert("Welcome again " + username);
    } else {
        username = prompt("Please enter your username:", "");
        if (username != "" && username != null) {
            setCookie("username", username, 365);
        }
    } } </script> </head> <body> <button id="open_market">Market</button> <script> checkCookie(); open_market.onclick = function() {   window.open("https://thenumnut.github.io/Wars-of-Shares/Market"); } </script> </body> </html>

It seems to be working fine

Comments