www.webdeveloper.com
Results 1 to 9 of 9

Thread: Using Javascipt to count number of times a code is run

  1. #1
    Join Date
    Aug 2009
    Location
    Ireland
    Posts
    25

    Using Javascipt to count number of times a code is run

    Hi everyone,

    I was wondering if anybody can provide me with high level suggestions as to how to do the following in javascript.

    I want to record the number of times a piece of javascript is run across multiple disparate machines.

    To do this I was thinking that the javascript should open javascript a webpage every time it is run. I can then count how many times that webpage has been called which in turn will be the number of times the javascipt has run.

    However I don't want the webpage to open in the users machine as this will be really annoying for the user.

    Does anyone have any suggestions on how to do this? or if you can think of a better way to complete the overall goal.

    Any help would be really appreciated!!

  2. #2
    Join Date
    Jan 2008
    Location
    Florida
    Posts
    1,227
    If you can use a server language your JS script can be placed in a PHP, ASP or CFM page where you can update a data table or set an application value. An external JavaScript file does not need to have a .js extension.

  3. #3
    Join Date
    Jan 2006
    Location
    Indiana, USA
    Posts
    79
    Are you working with a server language? If so, which one?

    You want the Javascript to load a page each time it does "something". What is the "something" it's doing? Is it launching another web page? Performing some calculation?

  4. #4
    Join Date
    Aug 2009
    Location
    Ireland
    Posts
    25
    Hi guys,

    Thanks so much for getting back to me. Sorry for omitting these key details in my original post.

    The javascript is sitting within a firefox extension which is comprised of just javascript.

    On right click the extension provides short cuts to a series of other web applications.

    I want to be able to track how many times the extension has been used so ie how many times has the javascript been run.

    Please let me know if more information is required here.

    Thanks

  5. #5
    Join Date
    Jan 2009
    Posts
    3,346
    You could create an invisible iframe or create a script element. Then change the src to your server-side script that counts usage.

  6. #6
    Join Date
    Aug 2009
    Location
    Ireland
    Posts
    25
    Thanks criterion9!

    Sorry I'm a bit of a newbie to this stuff.

    Does an I-Frame not have to still sit within an existing html page meaning that a html page would still have to be opened in the user's browser?

  7. #7
    Join Date
    Jan 2006
    Location
    Indiana, USA
    Posts
    79
    I'm sure that as soon as I post this, someone will prove me wrong, but I don't see how you can use a page as the counting mechanism as you suggest. It seems to me that the only way to do that would be to make the page do nothing but redirect to another target, in which case it functions merely as a pass-through and won't show to the user. The key there is, you have to be in the process of redirecting them to another page. It doesn't sound like that's what's happening, unless that's happening when they click on one of the links for the "other web applications". That's how it appears to me.

    There are other options, but they depend on your having access to a server side language. You must have a server involved, since you're able to serve up the counting page. If you can run a server language page there, e.g. ASP or PHP, you'd have a lot of options available to you for gathering a count. You could run an Ajax request to the page and update a counter somewhere, either in a text file, or a database.

  8. #8
    Join Date
    Jan 2009
    Posts
    3,346
    You can make an "invisible" iFrame by either making it 0 height and width or set it's style. An even easier solution would be to inject a script tag and set it's source to your counter server-side script. You'll just have to make sure that any output is valid javascript for that method to work.

  9. #9
    Join Date
    Jan 2009
    Posts
    3,346
    You could run an Ajax request to the page and update a counter somewhere, either in a text file, or a database.
    This will not be possible in this situation since most requests would be cross domain.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles