Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Thanked 0 Times in 0 Posts

    Question Help With 2 javascripts on one button


    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.

  2. #2
    Regular Coder
    Join Date
    Apr 2012
    St. Louis, MO
    Thanked 101 Times in 101 Posts
    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".

  3. Users who have thanked WolfShade for this post:

    edward260594 (12-16-2012)

  4. #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,699 Times in 2,675 Posts
    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:-
    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() {

    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.

  5. Users who have thanked Philip M for this post:

    edward260594 (12-16-2012)

  6. #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Sydney, Australia
    Thanked 932 Times in 919 Posts
    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.
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  7. #5
    New to the CF scene
    Join Date
    Dec 2012
    Thanked 0 Times in 0 Posts
    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

  8. #6
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    chertsey, a small town 25 miles south west of london, england.
    Thanked 527 Times in 513 Posts
    ...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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts