Reputation: 61
I have a javascript function embeded in my html code but when i'm running the page i get this error: Uncaught ReferenceError: myFunction is not defined.
I'm also using angularjs, so i have my index page and, as they explain, my partial page, and that functioon is in my partial page.
When i double click the web page from the folder i can run the script, but when i run the web api it present that error.
My javascript function:
<script type="text/javascript">
function myFunction() {
var x;
var r = confirm("Press a button!");
if (r == true) {
x = "You pressed OK!";
}
else {
x = "You pressed Cancel!";
}
document.getElementById("demo").innerHTML = x;
}
and how i call it:
<button onclick="myFunction()">Try it</button>
can anyone help me? thk
Upvotes: 0
Views: 75
Reputation: 1149
It seems it is trying to use the function even before it is loaded. Can you try below.
<script type="text/javascript">
function myLoad() {
var myButton = document.getElementById('myButton');
myButton.addEventListener('click', function() {
var x;
var r = confirm("Press a button!");
if (r == true) {
x = "You pressed OK!";
}
else {
x = "You pressed Cancel!";
}
document.getElementById("demo").innerHTML = x;
}, false);
}
window.load = myLoad;
</script>
Finally add an Id to the button.
<button id="myButton">Try it</button>
Upvotes: 2