Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-12-2012, 04:07 PM   PM User | #1
edward260594
New to the CF scene

 
Join Date: Dec 2012
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
edward260594 is an unknown quantity at this point
Question Help With 2 javascripts on one button

Hi

I am trying to create a webpage that allows the user to purchase some tickets, ive got 2 javascripts within my webpage, one that validates the text fields and one that uses the .write function , to show the person the total price. both of the scripts are fine as when run individualy they work, but when used together only the validation works and the other script seams to stop working. Any sugestions would be helpfull.

PS: one javascript is external and the other is within the webpage.

Many Thanks

Last edited by edward260594; 12-12-2012 at 04:13 PM..
edward260594 is offline   Reply With Quote
Old 12-12-2012, 04:46 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
WolfShade is an unknown quantity at this point
Firstly, if you're using a script that uses document.write, it's ancient and should be chucked in the round file drawer.

Check your error console for messages. Most likely the first script is breaking, which would render any other function that runs after it stopped.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Users who have thanked WolfShade for this post:
edward260594 (12-16-2012)
Old 12-12-2012, 05:45 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,032
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
There are two reasons why multiple scripts in the same page will not work together.

a) duplicate global variable and/or function names (including loop counters etc.)
b) multiple onload statements.
Have a look at:-
http://www.javascriptkit.com/javatut...iplejava.shtml
http://www.dyn-web.com/tutorials/combine.php
Or you can simply fire a set of functions when the page loads (even if there is only one script - unobtrusive js - rather than <body onload = ...)
<script type="text/javascript">
window.onload = function() {
functionOne();
functionTwo();
}
</script>

document.write() is in effect obsolete. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it).
So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.


Jesus saves, Moses invests, Allah protects, and Cthulhu thinks you'd make a nice sandwich.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
edward260594 (12-16-2012)
Old 12-12-2012, 06:22 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
If you wrap each script inside an anonymous function
If you use strict JavaScript and don't declare any variables globally
If you use event listeners instead of event handlers

then the situations where two scripts clash will be reduced to actual scripts are trying to do things that themselves clash - such as one script trying to turn an element blue while the other script is trying to turn it pink.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 12-16-2012, 05:40 PM   PM User | #5
edward260594
New to the CF scene

 
Join Date: Dec 2012
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
edward260594 is an unknown quantity at this point
Sorry for late reply, didnt realise how fast you guys respond ye i thought .write was a bit iffy but my teacher insisted it was okay. I think im going to make the total price update live and make the validation on submit. Once again thanks for fast responses
edward260594 is offline   Reply With Quote
Old 12-16-2012, 06:01 PM   PM User | #6
coothead
Senior Coder

 
coothead's Avatar
 
Join Date: Jan 2004
Location: chertsey, a small town 25 miles south west of london, england.
Posts: 1,543
Thanks: 0
Thanked 195 Times in 191 Posts
coothead will become famous soon enough
Quote:
...but my teacher insisted it was okay....

Teachers, if they're not telling us to use tables for page layouts,
then they're telling us to use document.write() in our javascript.

Where would we be without them.
coothead
coothead is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:54 PM.


Advertisement
Log in to turn off these ads.