1

I am working with a CMS system and have restrictions on where I can place code (at the moment). The script I'm trying to use must load after a form and it works successfully in Dreamweaver. But now that I'm moving things into the CMS I must run the script in the header instead of after the form. I'm attempting to load it using after the page loads like so:

<script> $(document).ready(function() { //You should create the validator only after the definition of the HTML form var frmvalidator = new Validator("leadform"); frmvalidator.EnableOnPageErrorDisplaySingleBox(); frmvalidator.EnableMsgsTogether(); frmvalidator.addValidation("first_name","req","Please enter your first name."); frmvalidator.addValidation("first_name","maxlen=40","Max length for first name is 40."); frmvalidator.addValidation("first_name","alpha","Please enter your first name."); frmvalidator.addValidation("last_name","req","Please enter your last name."); frmvalidator.addValidation("last_name","maxlen=40","Max length for last name is 40."); frmvalidator.addValidation("last_name","alpha","Please enter your last name."); frmvalidator.addValidation("email","req","Please enter your e-mail address."); frmvalidator.addValidation("email","email","Please enter your e-mail address."); frmvalidator.addValidation("state","dontselect=0","Please select your state."); }); </script> 

I have to believe that I'm just writing this all wrong but I can't find any examples that will help me do this. Clearly I'm a noob coder - any help would be appreciated.

Thanks.

5
  • To clarify - I need the above script to load so that it comes into effect after the form loads in the browser. Commented Apr 16, 2013 at 16:22
  • 2
    If you are using .ready() it will load after the form loads Commented Apr 16, 2013 at 16:23
  • 1
    Are you getting any error messages in the console? When you say this isn't working, how so? Commented Apr 16, 2013 at 16:24
  • colestrode is right. Are you using Firebug or any other development console? Commented Apr 16, 2013 at 17:38
  • What CMS are you using Robert? Commented Apr 16, 2013 at 17:41

1 Answer 1

2
$(document).ready(function(event){ // insert code here }); 

The above code will always wait until the document is loaded (which means all html including your form is loaded into the page) no matter whether it is in the head or in the body of your html. You just need to make sure that when you are using jQuery you put the script tag with your jQuery code after the script tag that loads jQuery. Here is an example that loads version 1.9.1 of jQuery:

<script src="http://code.jquery.com/jquery-1.9.1.js"></script> <!-- make sure your code is added after jQuery --> <script src='pathtoyourscript/yourScript.js'></script> 
Sign up to request clarification or add additional context in comments.

4 Comments

One thing your should be aware of, though, is that the ready event of the document occurs after all html is loaded, but NOT after all images are finished loading. In other words, document ready can trigger before all the images have loaded. If you want to execute code after images have loaded, you can use the load event of the window object. This doesn't have anything to do with your specific question; I just thought I'd throw that warning out there.
It also doesn't wait for ajax requests to fire, which i suspect is the real problem here.
Yeah, the page might be loaded via ajax.
thank you! After looking things over further I found I was on the right track just had some errors in the code in a couple of places on the page. I ended up setting all of my code in an external JS file which also helped me clean up my act.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.