Element creation and insertBefore not working

Okay, I hit my head against the wall and I don't know why this doesn't create the element. Maybe something very small that I am missing here. Basically, there is this Javascript code that is outputted in a PHP document, for example, somewhere in the middle of a page load, NOW unfortunately it cannot get into the header. While I'm not sure if this is the problem anyway, it might be ... hmmmmm.

// Setting the variables needed to be set.
    echo '
        <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/shoutbox.js"></script>';
    echo '
        <script type="text/javascript">
            var refreshRate = ', $params['refresh_rate'], ';
            createEventListener(window);
            window.addEventListener("load", loadShouts, false);

            function loadShouts()
            {
                var alldivs = document.getElementsByTagName(\'div\');
                var shoutCount = 0;
                var divName = "undefined";

                for (var i = 0; i<alldivs.length; i++)
                {
                    var is_counted = 0;
                    divName = alldivs[i].getAttribute(\'name\');

                    if (divName.indexOf(\'dp_Reserved_Shoutbox\') < 0 && divName.indexOf(\'dp_Reserved_Counted\') < 0)
                        continue;
                    else if(divName == "undefined") 
                        continue;
                    else
                    {
                        if (divName.indexOf(\'dp_Reserved_Counted\') == 0)
                        {
                            is_counted = 0;
                            shoutCount++;
                            continue;
                        }
                        else
                        {
                            shoutCount++;
                            is_counted = 1;
                        }
                    }

                    // Empty out the name attr.
                    alldivs[i].name = \'dp_Reserved_Counted\';

                    var shoutId = \'shoutbox_area\' + shoutCount;

                    // Build the div to be inserted.
                    var shoutHolder = document.createElement(\'div\');
                    shoutHolder.setAttribute(\'id\', [shoutId]);
                    shoutHolder.setAttribute(\'class\', \'dp_control_flow\');
                    shoutHolder.style.cssText = \'padding-right: 6px;\';
                    alldivs[i].parentNode.insertBefore(shoutHolder, alldivs[i]);

                    if (is_counted == 1)
                    {
                        startShouts(refreshRate, shoutId);
                        break;
                    }
                }
            }

        </script>';

      

Also, I'm pretty sure the other functions I link to in these functions are working fine. The problem here is that in this function the div is never created at all and I can't figure out why? Also, Firefox FireBug tells me that the divName variable is undefined, although I tried to take care of this in a function, although I'm not sure why.

Anyway, I need the generated div element to be inserted just before the following HTML:

echo '
            <div name="dp_Reserved_Shoutbox" style="padding-bottom: 9px;"></div>';

      

I am using name instead of id here because I don't want to duplicate id values, so I change the name value and increment it since this function can be called more than once. For example, if there are 3 screamboxes on the same page (don't ask why ... lol), I need to skip the other names I already changed to "dp_Reserved_Counted" which I believe I am doing correctly. Anyway, if I could put this in the header and call it only once, but that is not possible since they are loaded and cannot tell which one they are, so it is directly hardcoded into the actual output on the page of where the shoutbox is inside the HTML. Basically, not sure if this is the problem or not, but there should be some work to do if the problem is not in my code above ... arrg

Please help me. I actually need a second set of eyes. Thanks:)

+2


a source to share


3 answers


There are a number of problems with the loadShouts method. First, it's a comparison of the string "undefined" instead of a direct boolean test that will match. I've also removed a bunch of unnecessary logic. In addition, the id attribute assigned to the new shoutHolder was passed as an array rather than a direct property assignment. See if the following improves.



function loadShouts()
{
  var alldivs = document.getElementsByTagName("div");
  var shoutCount = 0;
  var divName = "undefined";

  for (var i = 0; i<alldivs.length; i++)
  {
    divName = alldivs[i].getAttribute("name");

    if (!divName)
      continue;
    if (divName.indexOf("dp_Reserved_Shoutbox") < 0 && divName.indexOf("dp_Reserved_Counted") < 0)
      continue;

    shoutCount++;    
    if (divName.indexOf("dp_Reserved_Counted") == 0)
      continue;

    // Empty out the name attr.
    alldivs[i].setAttribute("name", "dp_Reserved_Counted");

    var shoutId = "shoutbox_area" + shoutCount;

    // Build the div to be inserted.
    var shoutHolder = document.createElement("div");
    shoutHolder.setAttribute("id", shoutId);
    shoutHolder.setAttribute("class", "dp_control_flow");
    shoutHolder.style.cssText = "padding-right: 6px;";
    alldivs[i].parentNode.insertBefore(shoutHolder, alldivs[i]);

    startShouts(refreshRate, shoutId);
    break;
  }
}
      

0


a source


When you are testing divName

, switch the order of your conditions from this

                divName = alldivs[i].getAttribute(\'name\');

                if (divName.indexOf(\'dp_Reserved_Shoutbox\') < 0 && divName.indexOf(\'dp_Reserved_Counted\') < 0)
                    continue;
                else if(divName == "undefined") 
                    continue;

      

:



                var divName = alldivs[i].getAttribute(\'name\');
                if (!divName) // this is sufficient, by the way
                    continue;
                else if (divName.indexOf(\'dp_Reserved_Shoutbox\') < 0 && divName.indexOf(\'dp_Reserved_Counted\') < 0)
                    continue;

      

The problem is that when the script finds a div with no name, it tries to call the indexOf property of the non-existent value and hence throws an error.

+2


a source


Okay, just wanted to let you know how it went. And I thank you both for Tracker1 and Casey Hope. Especially Tracker for great feature rewriting. You are all ROCK. Here's the last function I use bytheway, just a small part of the Tracker1 Answer editing, that's why you got my votes !!

echo '
    <script type="text/javascript">
        var refreshRate = ' . $params['refresh_rate'] . ';
        createEventListener(window);
        window.addEventListener("load", loadShouts, false);

        function loadShouts()
        {
          var alldivs = document.getElementsByTagName("div");
          var shoutCount = 0;
          var divName = "undefined";

          for (var i = 0; i<alldivs.length; i++)
          {
            divName = alldivs[i].getAttribute("name");

            if (!divName)
              continue;
            if (divName.indexOf("dp_Reserved_Shoutbox") < 0 && divName.indexOf("dp_Reserved_Counted") < 0)
              continue;

            shoutCount++;    
            if (divName.indexOf("dp_Reserved_Counted") == 0)
              continue;

            // Empty out the name attr.
            alldivs[i].setAttribute("name", "dp_Reserved_Counted");

            var shoutId = "shoutbox_area" + shoutCount;

            // Build the div to be inserted.
            var shoutHolder = document.createElement("div");
            shoutHolder.setAttribute("id", shoutId);
            shoutHolder.setAttribute("class", "dp_control_flow");
            shoutHolder.style.cssText = "padding-right: 6px;";
            alldivs[i].parentNode.insertBefore(shoutHolder, alldivs[i]);

            startShouts(refreshRate, shoutId);
            break;
          }
        }

    </script>';

      

Thanks again for being BETTER!

0


a source







All Articles