The function I'm calling does not appear, what's wrong?

1

HTML     

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
        <script src="mostrarCreditos.js"></script>
    <meta charset="utf-8" />
    <title>Créditos</title>
        <input onclick="paraMenu()" type="button" value="ok" />

</head>
<body onload="creditosfinal()">
</body>
</html>

JavaScript

function creditosfinal(){
    var nome1 = prompt("Antonio Lucas Rodrigues Franceschini RA: 15688724");
    var nome2 = prompt("Lucas Oliveira Dos Santos RA: 15677735");

    document.write(+ nome1 + "<br/>" + nome2 +);

    var btn = document.createElement("BUTTON");
    var t = document.createTextNode("OK");
    btn.onclick = paraMenu;
    btn.appendChild(t);
    document.body.appendChild(btn);
}

function paraMenu()
{
    window.location.href = "menu.html";
}
    
asked by anonymous 01.12.2015 / 15:26

2 answers

2

Friend, I took the liberty of adjusting some things in your script, I hope you can add in your application.

// Adiciona um evento ao document para quando a pagina estiver carregada. Substitui o onload.
document.addEventListener('DOMContentLoaded', creditosFinal());

// Função que ao carregar a pagina cria os elementos no Body.
function creditosFinal() {
    var nome1 = 'Prompt não funciona aqui.'//prompt('Antonio Lucas Rodrigues Franceschini RA: 15688724');
    var nome2 = 'Prompt não funciona aqui.'//prompt('Lucas Oliveira Dos Santos RA: 15677735');
    
    document.body.appendChild(pNome(nome1,'p'));
    document.body.appendChild(pNome(nome2,'p'));
    document.body.appendChild(pNome('OK','button', 'click', paraMenu));
}

// Esta função retorna um novo elemento HTML.
function pNome(text, tipo, event, fn) {
    var elem = document.createElement(tipo);
    elem.textContent = text;
    if(event && fn) {
         elem.addEventListener(event, fn);
    }
    return elem;
}

// Função de redirecionamento.
function paraMenu() {
    // Redireciona para o menu.
    console.log('evento');
}
  

See working on    jsfiddle

One observation is that visual elements should be declared within the <body> tag such as <input> that was declared in <header> .

    
01.12.2015 / 16:17
0

windows.location.href is not a method, but a property that will tell you the current URL of the browser and change the value of this property will redirect the page.

window.open() is a method that you can pass the URL you want to open a new tab, for example;

Example window.location.href:

window.location.href = "http://www.google.com". // Vai te redirecionar para o google"

window.open () example:

window.open('http://www.google.com'); // Vai abrir o google em uma nova janela..

Additional information:

window.open () can be passed as an additional parameter, see: window.open tutorial

credits: Translated from topic

    
01.12.2015 / 15:51