• Resolved PKimpton

    (@pkimpton)


    I have a Javascript calculator I made a few years back which works well in a HTML page but when I add it to a WordPress page, it will not run.
    I have a smaller script in the same page which does work so I know I’m linking to .js file correctly. I have tried different methods of calling the Javascript code, all work with the smaller script, none with the bigger unless Im in a standard stand-alone HTML file.
    Any help would be very much appriciated.

    HTML Code

    <table>
    <tbody>
    <tr>
    <td><select id="mlen" name="mlen"><option value="2">2 Metres</option><option value="3">3 Metres</option><option value="4">4 Metres</option><option value="6">6 Metres</option><option value="8">8 Metres</option><option value="10">10 Metres</option><option value="12">12 Metres</option><option value="14">14 Metres</option><option value="16">16 Metres</option><option value="18">18 Metres</option></select></td>
    <td>by</td>
    <td><select id="mwid" name="mwid"><option value="2">2 Metres</option><option value="3">3 Metres</option><option value="4">4 Metres</option><option value="6">6 Metres</option><option value="8">8 Metres</option><option value="9">9 Metres</option><option value="10">10 Metres</option><option value="12">12 Metres</option><option value="14">14 Metres</option><option value="16">16 Metres</option><option value="18">18 Metres</option></select></td>
    </tr>
    </tbody>
    </table>
    
    <hr />
    
    <h3>Marquee Extra's</h3>
    <table>
    <tbody>
    <tr>
    <td>Image</td>
    <td>Main Marquee</td>
    <td></td>
    <td></td>
    <td>£<span id="totmar">0</span></td>
    </tr>
    <tr>
    <td>Image</td>
    <td>Lighting</td>
    <td></td>
    <td><input id="mlig" type="checkbox" name="mlig" /></td>
    <td>£<span id="totlig">0</span></td>
    </tr>
    <tr>
    <td>Image</td>
    <td>Flooring</td>
    <td></td>
    <td><input id="mflo" type="checkbox" name="mflo" /></td>
    <td>£<span id="totflo">0</span></td>
    </tr>
    <tr>
    <td>Image</td>
    <td>Heaters</td>
    <td></td>
    <td><input id="mhea" type="text" name="mhea" size="1" value="0" /></td>
    <td>£<span id="tothea">0</span></td>
    </tr>
    <tr>
    <td>Image</td>
    <td>Tables</td>
    <td></td>
    <td><input id="mtab" type="text" name="mtab" size="1" value="0" /></td>
    <td>£<span id="tottab">0</span></td>
    </tr>
    <tr>
    <td>Image</td>
    <td>Chairs</td>
    <td></td>
    <td><input id="mcha" type="text" name="mcha" size="1" value="0" /></td>
    <td>£<span id="totcha">0</span></td>
    </tr>
    </tbody>
    </table>
    
    <hr />
    <input type="button" onClick="mtot(mtot)" value="Calculate">
    <strong>Your estimate total is</strong>
    <strong>£<span id="mtot">0</span></strong>

    .js Code

    var pri = []; // Price structure.
              pri['mar'] = [10.00,7.50,6.50]; // Marquee prices.
              pri['lig'] = [1.00,0.75,0.50]; // Lighting prices.
              pri['flo'] = [3.00,2.50,2.00]; // Flooring prices.
    
          var priTab = 8.00; // Table price.
          var priCha = 2.00; // Chair price.
          var priHea = 50.00; // Heater price.
    
          function est() {
            var rat;
            var tot = 0;
            var squ = parseInt(el('mlen').options[el('mlen').selectedIndex].value) * parseInt(el('mwid').options[el('mwid').selectedIndex].value);
    
            if(squ > 100) rat = 2;
            else if(squ > 29) rat = 1;
            else rat = 0;
    
            var cMar = squ * pri['mar'][rat];
            el('totmar').innerHTML = cMar;
            tot += cMar;
    
            var tLig = el('totlig');
            if(el('mlig').checked == true) {
              var cLig = squ * pri['lig'][rat];
              tLig.innerHTML = cLig;
              tot += cLig;
            }
            else tLig.innerHTML = 0;
    
            var tFlo = el('totflo');
            if(el('mflo').checked == true) {
              var cFlo = squ * pri['flo'][rat];
              tFlo.innerHTML = cFlo;
              tot += cFlo;
            }
            else tFlo.innerHTML = 0;
    
            var Tab = parseInt(el('mtab').value);
            var tTab = el('tottab');
            if(Tab > 0) {
              var cTab = Tab * priTab;
              tTab.innerHTML = cTab;
              tot += cTab;
            }
            else tTab.innerHTML = 0;
    
            var Cha = parseInt(el('mcha').value);
            var tCha = el('totcha');
            if(Cha > 0) {
              var cCha = Cha * priCha;
              tCha.innerHTML = cCha;
              tot += cCha;
            }
            else tCha.innerHTML = 0;
    
    // Heater Settings
    
            var Hea = parseInt(el('mhea').value);
            var tHea = el('tothea');
            if(Hea > 0) {
              var cHea = Hea * priHea;
              tHea.innerHTML = cHea;
              tot += cHea;
            }
            else tHea.innerHTML = 0;
    
    // Total
    
            el('mtot').innerHTML = tot;
          }
    
          function el('ele') {
            return document.getElementById(ele);
          }
    
          window.onload = function() {
            el('mlen').onchange = est;
            el('mwid').onchange = est;
            el('mlig').onclick = est;
            el('mflo').onclick = est;
            el('mhea').onkeyup = est;
            el('mtab').onkeyup = est;
            el('mcha').onkeyup = est;
            est();
          }
Viewing 8 replies - 1 through 8 (of 8 total)
  • XavierBloemen

    (@xavierbloemen)

    I think it’s a problem with how you load your js file.

    look into enqeue style

    http://codex.wordpress.org/Function_Reference/wp_enqueue_script

    Thread Starter PKimpton

    (@pkimpton)

    Ive come across this page a few times but it seems to go right over my head.

    I’m placing a command,
    <script type=”text/javascript” src=”/js/prices.js”></script>
    in the Header.php page and the HTML code directly into the wordpress page.
    Am I missing something?

    XavierBloemen

    (@xavierbloemen)

    yes the javascript runs before the rest of the html is loaded.

    The javascript should be in the footer.php

    Thread Starter PKimpton

    (@pkimpton)

    Do I just place;
    <?php wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); ?>
    into my header?

    or <script type=”text/javascript” src=”/js/prices.js”></script>
    in the footer?

    XavierBloemen

    (@xavierbloemen)

    no you can post the script tag into footer.php

    Thread Starter PKimpton

    (@pkimpton)

    Ahhhh 🙂
    Thanks ever so much
    Ive been banging my head on this for nearly 4 hours!
    I have :

    </body>
    <script type=”text/javascript” src=”http://www.climateuk.com/js/prices.js”></script&gt;
    </html>

    and this has solved it.
    Well done, and thanks again.

    XavierBloemen

    (@xavierbloemen)

    no problem man glad i could help!

    Thread Starter PKimpton

    (@pkimpton)

    I have found the main issue. I have the Simple Google Map plugin installed and was loading a map in the sidebar on the same page. When I turned this off my script worked! Ive now installed Dynamic Widgets plugin and turned off the map on this page and I can return my javascript to the page rather than having it load for every page using the footer page.

    Thanks for all your advice and for the nudge in the right direction.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘JavaScript Code will not run’ is closed to new replies.