Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    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".

  • Users who have thanked WolfShade for this post:

    edward260594 (12-16-2012)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,899
    Thanks
    203
    Thanked 2,531 Times in 2,509 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:-
    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.

  • Users who have thanked Philip M for this post:

    edward260594 (12-16-2012)

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,593
    Thanks
    0
    Thanked 645 Times in 635 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.
    Stephen
    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.

  • #5
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    2
    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

  • #6
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,694
    Thanks
    0
    Thanked 232 Times in 227 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.
    coothead


  •  

    Posting Permissions

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