• Resolved prof611

    (@prof611)


    One of the posts on my blog https://your-homeremedies.professorsopportunities.com/herbalremedies/ is getting this error when it appears on the home page, and the error prevents the script from functioning properly.

    Error: TypeError: sub is null
    Source File: https://your-homeremedies.professorsopportunities.com/include/index.js
    Line: 269

    But the same post, when it appears as a single post, does not have the error, and the script works perfectly. The coding which uses the script is identical on the home page and the single post. I wrote this script 8 years ago, and have been using it all that time, and this error never occurred before.

    Here is the HTML that fires the script:

    <div class="question">
      <p style="margin-top:30px;">Can you suggest natural herbs for depression?</p>
      <div style="display:inline;">
        <button class="show" style="margin-left:142px;">Answer</button>
        <div class="box" style="position:absolute; left:-1px; top:-17px; visibility:hidden;">
    <textarea readonly="readonly" id="area" rows="20" cols="150" style="width:342px; height:184px; margin-bottom:20px; background:#FFF;">
    
    St. John’s wort is an herbal remedy that
    has been scientifically tested, and
    found to be effective in treating mild
    to moderate depression. It works just as
    well as antidepressant drugs, and does
    not have as many side effects.
    
    However, your doctor should definitely
    be consulted before taking St. John’s
    wort, since 1) it is not effective in
    treating major depression, and 2) it can
    interfere with certain prescription or
    OTC drugs.
    
    </textarea><br />
          <span style="padding-left:132px;">
            < <a href="#" class="hide">Close Box</a> >
          </span>
        </div>
      </div>
    </div>

    The script itself is in another folder, and is called by javascript in the <head> section of the page: <script src=”https://your-homeremedies.professorsopportunities.com/include/index.js&#8221; type=”text/javascript”></script>. Here is the coding for the script:

    var count = 0;
    function submitForm()
    {
      // Submit Form
      // Copyright 2006 Professional Website Design.
      // For other useful scripts and tutorials, see
      // https://www.professorscodingcorner.com/
      // You may use or modify the script in any way
      // you want, but do not remove the first three
      // lines above. Although it's not required, I
      // would appreciate an email to let me know
      // the URL of the page where are using it.
    
      if (!document.getElementById) return false;
      var sub = document.getElementById("submit");
      var form = document.getElementById("form");
      var val = sub.getAttribute("value");		[ My Note: this is line #269 ]
      sub.onclick = function()
      {
        var email = document.getElementById("email");
        if (email.value.length < 6)
        {
          alert("You forgot to enter your email address.");
          return false;
        }
        if (count == 0)
        {
          count++;
          var url = "https://your-homeremedies.professorsopportunities.com/cgi-bin/subscribe.cgi";
          form.setAttribute("action",url);
          sub.setAttribute("value","Please Wait");
          sub.style.backgroundColor = "#F00";
          form.submit;
          return;
        }
        return false;
      }
      return true;
    }

    I am completely at a loss. I can’t understand why the script works fine on the home page, but not on the single post – how can this be?

    And – most important – how can I fix the bug?


    Professor

Viewing 1 replies (of 1 total)
  • Thread Starter prof611

    (@prof611)

    I discovered the source of the bug described above. It came to me in a dream! It’s not a bug that anyone else will ever have, but it’s solution demonstrates a debugging principle that I was ignoring. So it may be applicable to others in a general sense.

    The debugging principle: if a script works on page 1, but not on page 2, then there must be something on page 2 that’s interfering with the script.

    In my case, the interfering script was the submitForm script. It was being used only on the Home page. So I deleted it, and the posts using the question & answer format worked properly. ( Now I have to rewrite the deleted script, so it doesn’t cause any interference – but that’s another problem altogether. )

Viewing 1 replies (of 1 total)
  • The topic ‘Javascript Error: sub is null’ is closed to new replies.