maanantai 5. toukokuuta 2014

Simple javascript yes/no prompt and html forward

I needed a simple confirmation YES/NO on my web page which would then redirect to a different page depending on the answer. I couldn't use any other languages but javascript so this is how I acquired it.

First I created a simple button

<button type="button" class="button" onclick="myFunction()">Press me please</button>

and the javascript

<script>
function myFunction()
{
var answer = confirm ("Yes")
if (answer)
window.location.assign("page1.html")
else
window.location.assign("page2.html")
}
</script>