Stop one javascript file from executing, from a different javascript file

1k views Asked by At

Is it possible to stop one javascript file from executing from a different javascript file?

Eg.

HTML

<!doctype html>
<html>
   <head>
      <title>jQuery UI Dialog functionality</title>
      <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
      <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
      <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      <!-- CSS -->

      <!-- Javascript -->
      <script src="checkout.js"></script>
      <script src="dummy.js"></script>

   </head>
   <body>
      Pickup Date: <input type="text" maxlength="10" name="Orders.Custom_Field_PickupDate" value=""> 
      <br />
      Return Date: <input type="text" maxlength="10" name="Orders.Custom_Field_ReturnDate" value=""> 
      <br />

        <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#EEEEEE" id="table_checkout_cart0">
            <tr>
                <td align="left">
                    <br>
                </td>
            </tr>
            <tr>
                <td> <span id="span_Shopping_Cart_UnEditable">
                <table border="0" cellpadding="10" cellspacing="1" id="v65-onepage-CartSummary">
                    <tr>
                        <td>
                            <table id="v65-onepage-ordersummary-items" border=0 cellpadding=2 cellspacing=1 width="580"><tr id="v65-onepage-ordersummary-header-row">
                                <td class="v65-onepage-ordersummary-itemcode v65-onepage-ordersummary-header" style="color:#000000;font-weight:bold">
                                    Code
                                </td>
                                <td class="v65-onepage-ordersummary-itemname v65-onepage-ordersummary-header" style="color:#000000;font-weight:bold">
                                    Name
                                </td>
                                <td class="v65-onepage-ordersummary-itemprice v65-onepage-ordersummary-header" style="color:#000000;font-weight:bold">
                                    Price
                                </td>
                                <td class="v65-onepage-ordersummary-itemqty v65-onepage-ordersummary-header" style="color:#000000;font-weight:bold">
                                    Qty
                                </td>
                                <td class="v65-onepage-ordersummary-itemtotal v65-onepage-ordersummary-header" style="color:#000000;font-weight:bold">
                                    Total
                                </td>
                    </tr>
            <tr>
                <td class="v65-onepage-ordersummary-itemcode" style="color:#666666">
                    K10306
                </td>
                <td class="v65-onepage-ordersummary-itemname" style="color:#666666">
                    MYT Works 4ft Large Slider Kit
                </td><td class="v65-onepage-ordersummary-itemprice" style="color:#666666">
                    $160.00
                    <br />
                    exc Tax
                </td>
                <td class="v65-onepage-ordersummary-itemqty" style="color:#666666">
                    1
                </td>
                <td class="v65-onepage-ordersummary-itemtotal" style="color:#666666">
                    $160.00
                    <br />10% Tax = $16.00
                </td>
            </tr>
            <tr>
                <td class="v65-onepage-ordersummary-itemcode" style="color:#666666">
                    C10881
                </td>
                <td class="v65-onepage-ordersummary-itemname" style="color:#666666">
                    Apurture V-Control USB Remote Focus
                </td>
                <td class="v65-onepage-ordersummary-itemprice" style="color:#666666">
                    $18.00
                    <br />
                    exc Tax
                </td>
                <td class="v65-onepage-ordersummary-itemqty" style="color:#666666">
                    1
                </td>
                <td class="v65-onepage-ordersummary-itemtotal" style="color:#666666">
                    $18.00
                    <br />
                    10% Tax = $1.80
                </td>
            </tr>
        </table>


      <div id="IncompletePickupDate-Dialog" title="Incomplete Pickup Date">Please enter pickup date.</div>
      <div id="IncompleteReturnDate-Dialog" title="Incomplete Return Date">Please enter return date.</div>
      <br />
      <input type="button" value="Place Order" id="btnSubmitOrder">
      <br />


   </body>
</html>

JS 01 - checkout.js

function checkItems() {
    var returnValue = 0;
    $('#v65-onepage-ordersummary-items tr').each(function() {
        $this = $(this);
        var code = $.trim($this.find(".v65-onepage-ordersummary-itemcode").html());
        if ( code.charAt(0) == 'K' ) {
            returnValue = 1;
            return false; 
        }
    });
    return returnValue;
}

$(function() {
    $('input[name="Orders.Custom_Field_PickupDate"]').datepicker({
         dateFormat:'dd/mm/yy', showButtonPanel: true
    });
    $('input[name="Orders.Custom_Field_ReturnDate"]').datepicker({
         dateFormat:'dd/mm/yy', showButtonPanel: true
    });
    $( "#IncompletePickupDate-Dialog").dialog({
        autoOpen: false,  
    });
    $( "#IncompleteReturnDate-Dialog").dialog({
        autoOpen: false,  
    });  
    $( "#btnSubmitOrder" ).click(function(e) {
        var pickupDate = $('input[name="Orders.Custom_Field_PickupDate"]').val().length;
        var returnDate = $('input[name="Orders.Custom_Field_ReturnDate"]').val().length;
        if (checkItems()) {
            if (pickupDate == 0) {
                e.preventDefault();
                $( "#IncompletePickupDate-Dialog" ).dialog( "open" );
            }
            else if (returnDate == 0) {
                e.preventDefault();
                $( "#IncompleteReturnDate-Dialog" ).dialog( "open" );
            }
        }   
        if ((pickupDate !=0 && returnDate != 0) || checkItems() == 0) {
            // TODO: Direct to Complete.html which displays just text saying Done.
        }
        console.log(checkItems());
    });
});

JS 02 - dummy.js

$(function() {
    $( "#btnSubmitOrder" ).click(function(e) {
        window.location.replace("http://www.google.com");
    });
});

What I want to do is, if pickupDate or returnDate is equal to zero (code in checkout.js), I want to stop all javascript files from running, and I don't want dummy.js to execute. If pickupDate and returnDate are not equal to 0, then I want to continue with the execution.

I am trying to help a friend during my uni break on his website. I can't combine the two files because the software is proprietary software, and there are a lot of limitations. I can't edit their code because it is hard coded, but I can upload html, css, javascript or jQuery files.

On his website he sells and rents products - all rental products start with the letter "K", and all sale products start with something else. On his checkout page he currently has to enter a pickup date and return date for sale and rental products, but he only wants users to enter pickup or return date for rental products.

The code I wrote above only executes when the "btnSubmitOrder" is clicked, and currently there is default functionality for this button, which I do not have access to. So to achieve this, I thought about putting a javascript file above the default functionality, and attempt what I want to do.

Note: Files above are just some testing I'm doing locally, the real files are too large to paste here.

If this does not make sense, or more info is required please let me know.

Thanks in advance.

0

There are 0 answers