简体   繁体   中英

string of a href with onclick inside

Im new to javascript and trying to code ping pong, following an outdated/semi-erroneous guide. The HTML file has a messagebox that this function writes into after someone scores a point.

function score(side) {
    clearInterval(t);
    msgBox = document.getElementById('messageBox');
    msgBox.style.visibility = "visible";
    scorePop = "<p> Point for the "+side+" side!<br/> ";
    scorePop += "To serve the ball again, press \"s\" after closing this pop-up.</p>";
    scorePop += "<a href="\"javascript:return false="" onclick="\prepNewGame('"left"');return"X Close</a>";
    msgBox.innerHTML = scorePop;
}

The passed argument is the winning side in the form of 'left' or 'right'. I know the problem is in the second to last line of code because if I comment it out I can run everything else. I believe there is something wrong with quotations in the string but I've stared at this for too many hours. Though I'm not exactly certain what the second to last line of code is meant to do, I believe it's meant to appear in the msgBox with a button to "X Close" and prompt prepNewGame at the same time. Once closed, the user can press "s" to start a new game. Here is prepNewGame just in case.

function prepNewGame(side) {
    t=0;
    msgBox.style.visibility='hidden';
    theBall.style.top = "10px";
    if(side == "left") {
        theBall.style.left = "25px";
        rpadl.style.top = "5px";
        lpad.style.top = "5px";
    } else {
        theBall.style.left = (winWidth-40) + "px";
        rpadl.style.top = "5px";
        lpadl.style.top = "5px";
    }
}

much simpler is to return false onclick:

scorePop += "<a href=\"#\" onclick=\"return prepNewGame('left');\">X Close</a>";

and in prepNewGame function:

function prepNewGame(side) {
    .....
    .....
    return false;
}

The second to last line of code attempts to start a new game when clicked. It returns false on the click event in order to prevent reloading the page.

javascript:return false;

I feel the issue may lie in the escaping of the double quotes, "\\"javascript:return false="" . Try using something along the lines of;

scorePop += "<a href='#' onclick='return prepNewGame(\"left\");">X Close</a>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM