The function I’m calling doesn’t appear, what’s wrong?

Asked

Viewed 119 times

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";
}
  • Good afternoon, Voce could post the rest of the code ?

  • 1

    I don’t understand why you use prompt.

  • 1

    Good afternoon, remove the + in this section Document.write(+ name1 + "<br/>" + Nome2 +) and : Document.write(name1 + "<br/>" + Nome2); Follow example -> https://jsfiddle.net/h_felix/1r194c6L/

2 answers

2

Friend, I took the liberty of adjusting a few 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 tag <body> such as the <input> which was declared in <header>.

0

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

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

window.location.href example:

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 additional parameter, See: window.open tutorial

credits: Translated from the topic

Browser other questions tagged

You are not signed in. Login or sign up in order to post.