0

I have a javascript code which uses iFrames to pull the data required. This is made in to a bookmarklet. I wanted to know if it is possible to count how many times this bookmarklet is clicked and if so, how to get the number of clicks in to a file? This is the original code:

javascript:(function () {
if (!$('#OmniBoxDiv').length) {
    var strLoad = '<div id="OmniBoxDiv" style="display: block;background-color: gold;font-size: 1.25em;z-index: 1000;position: fixed;width: 96%;padding: 2%;  text-align: center">Loading...</div>';
    var divLoad = $(strLoad).prependTo('body');
}

if(typeof OmniBox === 'object'){
    OmniBox.msg();
    return;
}
OmniBox = this;

var FStatus = $('tr:has(td:contains("FStatus")):eq(1)>td:eq(1)').text();
var MStatus = $('tr:has(td:contains("MStatus")):eq(2)>td:eq(1)').text();
var Flink = $('a:contains("F Profile")').attr('href');

    this.msg = function(){
            '<tr><td></td><td>Fstatus:</td><td>'+FStatus+'</td></tr>'+
        '<tr><td>IGC</td><td></td><td></td></tr>'
        str = '<table>' + str + '</table><a href="javascript:OmniBox.CloseOmniBox();" style="background-color: darkorange;display: inline-block;padding: 0.5% 1%;cursor: pointer;">Close</a>';
        $('#OmniBoxDiv').html(str);
    }
};
this.CloseOmniBox = function(){
    $('#OmniBoxDiv').remove();
};

var FCheck = false, MCheck = false; 
var IFF = $('<iframe>'), IFM = $('<iframe>');
$('body').append(IFF);$('body').append(IFM);$('body');

IFF.attr('id','IFF').css('display','none').attr('src',FLink).on('load',function(){
    "code"
    },
    function(){
        FCheck = true;
        msg();
    });

});

"code"
    },
    function(){
        MCheck = true;
        msg();
    });
});


});

function whilst (condition, action, final) {
    var handle = setInterval(function () {
        if (condition()) {
            action();
        } else {
            clearInterval(handle);
            final();
        }
    }, 500);
}
})();
Bogdan Burym
  • 5,482
  • 2
  • 27
  • 46
prabhu ram
  • 11
  • 2

2 Answers2

0

You can count the number of clicks on a page as follows. However remember that when you navigate away from that page you'll lose that count unless you use cookies to persist the count.

//cache count display element
var cClick = $('#click_count');

//set up click event handler for the page
$(document).on('click', function() {
    
    //retrieve, increment, store and display click count
    cClick.text( ++cClick.data()['count'] );

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Click Count: 
<span id="click_count" data-count="0">0</span>
PeterKA
  • 24,158
  • 5
  • 26
  • 48
0

You can use global variable in bookmarklet.

if(typeof count === number){
     count++;
} else {
     count=0;
}

You can choose more specific name for this variable to avoid conflicts on current page. Also you can use localStorage to save count if you want

Dmitry Masley
  • 525
  • 4
  • 9
  • Thank you for the answer, and how do i store the count to the local storage. Please don't get irritated if it is a stupid question , i am a novice programmer. And is it possible to add the above code to my source code and then create a bookmarklet? – prabhu ram May 30 '15 at 17:49
  • Note that different sites have different localStorages. I don't know if it that you need. And yes this code should work. – Dmitry Masley Jun 01 '15 at 14:51
  • The best approach is to make bookmarklet load your script (create `script` tag with proper src attribute and insert in ``). As alternative to ubove approach you can also use function, that counts number of calls – Dmitry Masley Jun 01 '15 at 15:05
  • http://stackoverflow.com/questions/7243101/function-count-calls also you can take a look on arguments.callee http://stackoverflow.com/questions/12050183/javascript-arguments-callee-what-is-it-for – Dmitry Masley Jun 01 '15 at 15:06