By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,685 Members | 1,681 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,685 IT Pros & Developers. It's quick & easy.

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

P: 1
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...
Expand|Select|Wrap|Line Numbers
  1. HTML
  2. <body onLoad=gen_hits()>
  3. ...
  4. <span id='hits'></span><SCRIPT language="JavaScript" SRC="http://www.mycompany.com/cgi-bin/counterdir/otherdir/counter"></SCRIPT>
  5. ...
  6. </body>
  7.  
  8. EXTERNAL JAVASCRIPT (ini.js)
  9. var hits="HITS ";
  10.  
  11. function gen_hits() {
  12. document.getElementById("hits").innerHTML=hits;
  13. }
  14.  
and here an "idea" of my request...
Expand|Select|Wrap|Line Numbers
  1. HTML
  2. <body onLoad=gen_hits()>
  3. ...
  4. <span id='hits'></span>
  5. ...
  6. </body>
  7.  
  8. EXTERNAL JAVASCRIPT
  9. var hits="HITS " + <SCRIPT language="JavaScript" SRC="http://www.mycompany.com/cgi-bin/counterdir/otherdir/counter"></SCRIPT>;
  10.  
  11. function gen_hits() {
  12. document.getElementById("hits").innerHTML=hits;
  13. }
  14.  
I also tried a piece of code using this method...
Expand|Select|Wrap|Line Numbers
  1.         function setInnerHTMLAndExecScript (element, html) {
  2.                         var newElement = element.cloneNode(false);
  3.                         newElement.innerHTML = html;
  4.                         element.parentNode.replaceChild(newElement, element);
  5.  
  6.                 }
  7.  
  8.         function gen_hits() {
  9.  
  10.                 var myHtml = '<script type="text\/javascript" SRC="http://www.mycompany.com/cgi-bin/counterdir/otherdir/counter"><\/script>';
  11.                 myHtml.type = 'text/javascript';
  12.                 setInnerHTMLAndExecScript(document.getElementById('hits'),myHtml);
  13.         }
  14.  
That works partially: in IE there's no result and in Firefox it finds,increments 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
Mar 7 '11 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
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.
Mar 9 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.