How would you have a certain Javascript snippet run only when a form is not being handled?
NickName:chromedude Ask DateTime:2011-09-18T10:16:36

How would you have a certain Javascript snippet run only when a form is not being handled?

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

More about “How would you have a certain Javascript snippet run only when a form is not being handled?” related questions

How would you have a certain Javascript snippet run only when a form is not being handled?

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

Show Detail

Temporarily stop form events from either being raised or being handled?

I have a ton on controls on a form, and there is a specific time when I want to stop all of my events from being handled for the time being. Usually I just do something like this if I don't want ce...

Show Detail

How can I run this JavaScript snippet only once when a user logs in?

I have this simple JavaScript snippet. &lt;script&gt; window.onload = function(){ swal("Hello!", "Welcome to Portal", "success"); }; &lt;/script&gt; How can I only run thi

Show Detail

Validate only parts of a form

I have a rather large form, that I've broken up into individual sections with "Next" and "Back" buttons for the user to navigate each step. Here is how I set it up to work for now: Each section is

Show Detail

Run javascript if AJAX responce is a certain length of characters

How would you run a segment of javascript if the AJAX response was a certain amount of characters? What I have so far is: $.ajax({ type: "POST", url: action, data: form_data, success: function(re...

Show Detail

How would you lock down an online form?

We have a web application that is based around a form that gets passed around. Unfortunately when people look at it, it is editable. So two people can edit it at the same time and overwrite their c...

Show Detail

How to style a Javascript snippet

I want to embed this Javascript snippet (webform from AWeber) into my website: &lt;script type="text/javascript" src="http://forms.aweber.com/form/49/522310949.js"&gt;&lt;/script&gt; My site uses ...

Show Detail

How would I capture the timestamp of when a link is clicked in an Email?

This may be a bit of a tricky one (for me at least, but you guys may be smarter). I need to capture the timestamp of exactly when a reader clicks a link in an email. However, this link is not a hyp...

Show Detail

How is the caret "^" being handled when doing a search with PrincipalSearcher?

Let's say I have a user called "John" in AD. When I try to search for "^John" (with caret) using PrincipalSearcher, I would get "John" as a result. I'd expect the result to be null since "^John" u

Show Detail

Making a Game how would i assign an image(sprite) to a specific object to have certain behaviours

For example a certain image on the screen how would correspond to an object so it would have certain values like how much damage or health it has Would you put a bufferedImage in the constructor of

Show Detail