How to Insert a counter call in external Javascript (innerHTML)?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gino Dicandio
    New Member
    • Mar 2011
    • 1

    #1

    How to Insert a counter call in external Javascript (innerHTML)?

    Hello,

    I actually use a counter on a webpage (This is an external script that reads and increments a value - It actually works)
    To do it, I use an inline javascript but I would like to unify the entire page and call that counter directly in the external Javascript that manages the whole site.

    Here's the actual code...
    Code:
    HTML
    <body onLoad=gen_hits()>
    ...
    <span id='hits'></span><SCRIPT language="JavaScript" SRC="http://www.mycompany.com/cgi-bin/counterdir/otherdir/counter"></SCRIPT>
    ...
    </body>
    
    EXTERNAL JAVASCRIPT (ini.js)
    var hits="HITS ";
    
    function gen_hits() {
    document.getElementById("hits").innerHTML=hits;
    }
    and here an "idea" of my request...
    Code:
    HTML
    <body onLoad=gen_hits()>
    ...
    <span id='hits'></span>
    ...
    </body>
    
    EXTERNAL JAVASCRIPT
    var hits="HITS " + <SCRIPT language="JavaScript" SRC="http://www.mycompany.com/cgi-bin/counterdir/otherdir/counter"></SCRIPT>;
    
    function gen_hits() {
    document.getElementById("hits").innerHTML=hits;
    }
    I also tried a piece of code using this method...
    Code:
            function setInnerHTMLAndExecScript (element, html) {
                            var newElement = element.cloneNode(false);
                            newElement.innerHTML = html;
                            element.parentNode.replaceChild(newElement, element);
    
                    }
            
            function gen_hits() {
                    
                    var myHtml = '<script type="text\/javascript" SRC="http://www.mycompany.com/cgi-bin/counterdir/otherdir/counter"><\/script>';
    				myHtml.type = 'text/javascript';
                    setInnerHTMLAndExecScript(document.getElementById('hits'),myHtml);
            }
    That works partially: in IE there's no result and in Firefox it finds,increment s and displays the value but then it's impossible to continue using the page, it seems never finishing loading, even reload or refresh don't work, just close the webpage and open a new one is possible.

    I asked a lot of people but no one seems to find a solution.
    Does someone have an idea on how to proceed ?

    Thanks in advance...
    Gino
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Does the external counter script output a value or does it set a value to a variable?

    You can dynamically add a script to the head of the document by getting the head element and appendChild to append it to the head.

    Comment

    Working...