I have a piece of Javascript I want to run when a form loads. The form handles itself. When the form loads the Javascript works great, but when the form is submitted and the page handles itself the Javascript breaks it.
Is there a way to make the Javascript only run when the form is not being handled?
UPDATE:
I get this error when the form is handled (the Javascript is doing a AJAX request):
TypeError: 'undefined' is not an object
XMLHttpRequest cannot load [ajax request url] Origin [original page] is not allowed by Access-Control-Allow-Origin
Here is the HTML:
<form method="#" action="post">
<!--form elements-->
</form>
Here is the Javascript:
ajax.onreadystatechange=function() {
if (ajax.readyState==4 && ajax.status==200) {
document.getElementById("invitestar-message").innerHTML=ajax.responseText;
var signup = document.getElementById("invitestar-content-container");
setTimeout(function(){
body.removeChild(document.getElementById('invitestar-verifying'));
signup.style.display = "block";
}, 500);
}
}
ajax.open("GET","http://nvrforget.com/invitestar/check-invite/?invite=kNdqyJTjcf",true);
ajax.send();
The domain that this javascript is run on is not http://nvrforget.com/
.
Copyright Notice:Content Author:「chromedude」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7459082/how-would-you-have-a-certain-javascript-snippet-run-only-when-a-form-is-not-bein