jeudi 15 février 2018

How to run JavaScript on a certain page? [duplicate]

This question already has an answer here:

I wrote a script to save content dynamically on a form. Currently I have the script included in my footer.

The script is made to save the content every 10 seconds seconds the only problem is the script is running on pages that it shouldn't run on.

How can I make it so this script only runs on a certain page and still have it included in my footer?

$(document).ready(function() {
  function checkObj(obj) {
    for (var key in obj) {
      if (obj[key] == '') {
        return false;
      }
    }

    return true;
  }

  function autoSave() {
    var obj = {
      section1: $('#ExecSum').val(),
      section2: $('#IndustryBusiness').val(),
      section3: $('#IBGCE').val(),
      section4: $('#IBPS').val(),
      section5: $('#IndustryAnalysis').val(),
      section6: $('#SWOTAnalysis').val(),
      section7: $('#MPThreats').val(),
      section8: $('#MP4P').val(),
      section9: $('#OperationalPlan').val(),
      section10: $('#LegalEnviron').val(),
      section11: $('#MoneyMgmt').val(),
      section12: $('#MLO').val()
    };


    if (checkObj(obj)) {
      $.ajax({
        url: "save.php",
        method: "POST",
        data: obj,
        dataType: "text",
        success: function(data) {
          if (data != '') {
            console.log(data);
            $('#post_id').val(data);
          }
          $('.autoSave').text("Saving...");
          setInterval(function() {
            $('.autoSave').text('');
          }, 5000);
        },
        error: function(stuff, status, e) {
          console.log("Error!\n" + stuff.responseText);
          console.log(status);
          console.log(e);
        }
      });
    }
  }
  setInterval(function() {
    autoSave();
  }, 10000);
});




Aucun commentaire:

Enregistrer un commentaire