Jump to content

Running javascript mid-page.


smithmr8

Recommended Posts

Hello,

I've got a form validation script, which currently runs when either a field is edited or submitted. I'd like to have it run when the page is loaded.

 

Currently, I am using this for the running whilst submitted..

<form onSubmit="return checkForm();"...

 

I am using the onChange event to run the same function when any field is changed.

 

I'd like to be able to run the function at the load of the page, so its already telling the person what fields need to be filled in.

 

Would greatly appreciate some help..

 

Regards,

Luke

Link to comment
Share on other sites

Ah, yeah, but I don't really want it to load on every page. Although, I suppose it doesn't really matter.

 

I don't suppose there is a way to get the name of the current page you are looking at is there ? So I can only put that in the body tag when its on that page.

Link to comment
Share on other sites

OK, I am assuming from your response that you have default code that creates the head and <BODY> tag for the page. It is only in the content of the page that you would *know* what page is being loaded?

 

Well, that is easy enough to work around. You could simple insert this bit of code within the code that builds the form

<script type="text/javascript">
window.onload = function()
{
    checkForm();
}
</script>

 

But, I think this is a poor approach. JavaScript validation is a great tool to give the user some immediate feedback, but your pages should never totally rely upon JavaScript - especially for validation. I'm assuming you want to validate the page when it loads because some fields will already be populated. I can only think of that occuring if you have server-side validation and are redisplaying the form after server-side validation fails. Well, server-side validation should always be in place (in case user has JS turned off). But, if user has JS turned off and they submit invalid data, what would be the point of returning them to the form and trying to use JS to tell them their errors. You should use the server-side code to display the erros of any data that has been submitted.

Link to comment
Share on other sites

I understand that. There is server side validation aswell as the javascript. The JS is just displaying visual notificiations beside fields as to whether the fields are filled in correctly.

I only want to run the function on load, as it will display the messages to start with.. instead of them displaying for everything when someone edits a field as it doesn't look too good.

Link to comment
Share on other sites

I only want to run the function on load, as it will display the messages to start with.. instead of them displaying for everything when someone edits a field as it doesn't look too good.

 

It's your call, but I would reate my server-side validation to output the page with the same error messages/states that the JavaScipt validation would do. Then you don't need to run the JS validation onload. Again, if you are running server-side validation to catch errors for users w/o JS, what would those users see in the way of error messages if you are relying on the JS to diplay the errors?

Link to comment
Share on other sites

Well, seeing as the form is only submitted when the JS returns a true value. If JS wasn't enabled and the form was submitted incorrectly, it would return to the form, with the fields still containing the submitted values and the error messages displaying where the JS ones do, except this is server side.

Link to comment
Share on other sites

Well, seeing as the form is only submitted when the JS returns a true value.

If the user does not have JS enabled, the form will always be submitted. The form can't check if a true or false value is returned if JS is not enabled.

 

f JS wasn't enabled and the form was submitted incorrectly, it would return to the form, with the fields still containing the submitted values and the error messages displaying where the JS ones do, except this is server side.

Not sure I follow the last bit. Are you saying the server-side code is generating error messages to be displayed? If so, I would agree that is a good process. Based upon your request it appeared you wanted the JS to run on load of the page to display the error messages, which wouldn't make sense if the form was submitted with errors because the user did not have JS enabled.

 

Again, it's your call. I'm just giving my two cents. Good luck.

Link to comment
Share on other sites

I understand the form would be submitted regardless when JS is disabled. This is exactly the reason I have server side validation to do the same thing as my JS validation, except without the lovely dynamic front-end. I just want to have JS as it makes things look so much more professional, which is exactly the look I'm going for.

Link to comment
Share on other sites

I don't suppose there is a way to get the name of the current page you are looking at is there ? So I can only put that in the body tag when its on that page.

 

Think that comes down to the design of your site.

 

For example on a phpBB based site I was building I set up a link to a pages javascript as a template field in the header, and only populated on those pages that required it, just ignoring it on most other pages even though they used the same template. Nothing to stop the javascript file from using window.onload (or variations on it to allow multiple onload functions).

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.