Two variables onclick event

1

I have the following problem, I am trying to put a push in an onclick event before giving the value to window.location .

 href="javascript:;" onclick="window.location = 'pagina.php';"

But, the problem that I have, is that if I put it ahead with ',' or with ';', in the code that is generated, I see that it appears, but the redirection to the page is lost.

 href="javascript:;" onclick="variable.push('4'),window.location = 'pagina.php';"

Then I do not know how to put it ..... I have tried several ways, but the onclick always gets annoyed.

Thanks and best regards

    
asked by Imanol 02.05.2016 в 10:18
source

2 answers

1

Why not use a .js file?

I think it's better than using javascript like:

document.getElementById("clickme").addEventListener("click", function()
{
	console.log("primero");
    window.location="http://www.google.com";
});
<a href="#" id="clickme">
  Hello
</a>

When only one .html file is used, it is possible to write javascript with the tag 'script'

 <script type="text/javascript">
     document.getElementById("clickme").addEventListener("click",    function()
     {
    console.log("primero");
    window.location="http://www.google.com";
     });
</script>

But, it's better than using one file for .html and one file for .js.

    
answered by 02.05.2016 в 11:03
1

The problem is because the variable is not defined in the onclick.

I'll give you an example <a href="#" onclick="var variante=[];variante.push('4');alert(JSON.stringify(variante));window.location.href='www.google.com'">Boton de prueba</a>

    
answered by 29.01.2017 в 01:04