【问题标题】:Is it possible to count how many times this bookmarklet is clicked and if so, how to get the number of clicks in to a file?是否可以计算此书签被点击了多少次,如果可以,如何获得文件的点击次数?
【发布时间】:2015-05-29 15:15:56
【问题描述】:

我有一个 JavaScript 代码,它使用 iFrames 来提取所需的数据。这是在书签中制作的。我想知道是否可以计算这个书签被点击了多少次,如果可以,如何获得文件的点击次数?这是原始代码:

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);
}
})();

【问题讨论】:

    标签: javascript jquery iframe


    【解决方案1】:

    您可以按如下方式计算页面上的点击次数。但是请记住,当您离开该页面时,您将丢失该计数,除非您使用 cookie 来保存计数。

    //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>

    【讨论】:

    • 您不能将本地存储用于小书签,因为来源会因页面而异。
    【解决方案2】:

    您可以在书签中使用全局变量。

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

    您可以为该变量选择更具体的名称以避免当前页面上的冲突。 如果需要,您也可以使用localStorage 来保存计数

    【讨论】:

    • 感谢您的回答,以及如何将计数存储到本地存储。如果这是一个愚蠢的问题,请不要生气,我是新手程序员。是否可以将上述代码添加到我的源代码中,然后创建一个书签?
    • 注意不同的站点有不同的localStorages。我不知道你是否需要它。是的,这段代码应该可以工作。
    • 最好的方法是让小书签加载你的脚本(创建script标签并插入&lt;head&gt;)。作为 ubove 方法的替代方法,您还可以使用计算调用次数的函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多