click event firing multiple times in jQuery plugin -


i using jquery plugin called printelement:

http://projects.erikzaadi.com/jqueryplugins/jquery.printelement/

it's working on first click should upon further clicks (after closing pop window) continues pop windows. on second click pops 4 times, on third click i'm lucky enough 36 pop ups. each show after i've closed previous pop window. i've tried bind() , unbind() , event.stoppropagation() without change.

here call it:

$('a[href="#print"]').click(function(){$("body").printelement();}); 

and here plugin code:

/// <reference path="http://code.jquery.com/jquery-1.4.1-vsdoc.js" /> /* * print element plugin 1.2 * * copyright (c) 2010 erik zaadi * * inspired printarea (http://plugins.jquery.com/project/printarea) , * http://stackoverflow.com/questions/472951/how-do-i-print-an-iframe-from-javascript-     in-safari-chrome * *  home page : http://projects.erikzaadi/jqueryplugins/jquery.printelement  *  issues (bug reporting) :      http://github.com/erikzaadi/jqueryplugins/issues/labels/printelement *  jquery plugin page : http://plugins.jquery.com/project/printelement  *   *  david b (http://github.com/ungenio) , icgjohn  (http://www.blogger.com/profile/11881116857076484100) *  great contributions! *  * dual licensed under mit , gpl licenses: *   http://www.opensource.org/licenses/mit-license.php *   http://www.gnu.org/licenses/gpl.html *    *   note, iframe printing not supported in opera , chrome 3.0, popup window shown instead */ ; (function (window, undefined) { var document = window["document"]; var $ = window["jquery"]; $.fn["printelement"] = function (options) {     var mainoptions = $.extend({}, $.fn["printelement"]["defaults"], options);     //iframe mode not supported opera , chrome 3.0 (it prints entire page).     //http://www.google.com/support/forum/p/webmasters/thread?tid=2cb0f08dce8821c3&hl=en     if (mainoptions["printmode"] == 'iframe') {         if ($.browser.opera || (/chrome/.test(navigator.useragent.tolowercase())))             mainoptions["printmode"] = 'popup';     }     //remove printed iframe if exists     $("[id^='printelement_']").remove();      return this.each(function () {         //support metadata plug-in if available         var opts = $.meta ? $.extend({}, mainoptions, $(this).data()) : mainoptions;         _printelement($(this), opts);     }); }; $.fn["printelement"]["defaults"] = {     "printmode": 'iframe', //usage : iframe / popup     "pagetitle": '', //print page title     "overrideelementcss": null,     /* can 1 of following 3 options:     * 1 : boolean (pass true stripping css linked)     * 2 : array of $.fn.printelement.csselement (s)     * 3 : array of strings paths alternate css files (optimized print)     */     "printbodyoptions": {         "styletoadd": 'padding:10px;margin:10px;', //style attributes add body of print document         "classnametoadd": '' //css class add body of print document     },     "leaveopen": false, // in case of popup, leave print page open or not     "iframeelementoptions": {         "styletoadd": 'border:none;position:absolute;width:0px;height:0px;bottom:0px;left:0px;', //style attributes add iframe element         "classnametoadd": '' //css class add iframe element     } }; $.fn["printelement"]["csselement"] = {     "href": '',     "media": '' }; function _printelement(element, opts) {     //create markup printed     var html = _getmarkup(element, opts);      var popuporiframe = null;     var documenttowriteto = null;     if (opts["printmode"].tolowercase() == 'popup') {         popuporiframe = window.open('about:blank', 'printelementwindow', 'width=650,height=440,scrollbars=yes');         documenttowriteto = popuporiframe.document;     }     else {         //the random id overcome safari bug http://www.cjboco.com.sharedcopy.com/post.cfm/442dc92cd1c0ca10a5c35210b8166882.html         var printelementid = "printelement_" + (math.round(math.random() * 99999)).tostring();         //native creation of element faster..         var iframe = document.createelement('iframe');         $(iframe).attr({             style: opts["iframeelementoptions"]["styletoadd"],             id: printelementid,             classname: opts["iframeelementoptions"]["classnametoadd"],             frameborder: 0,             scrolling: 'no',             src: 'about:blank'         });         document.body.appendchild(iframe);         documenttowriteto = (iframe.contentwindow || iframe.contentdocument);         if (documenttowriteto.document)             documenttowriteto = documenttowriteto.document;         iframe = document.frames ? document.frames[printelementid] : document.getelementbyid(printelementid);         popuporiframe = iframe.contentwindow || iframe;     }     focus();     documenttowriteto.open();     documenttowriteto.write(html);     documenttowriteto.close();     _callprint(popuporiframe); };  function _callprint(element) {     if (element && element["printpage"])         element["printpage"]();     else         settimeout(function () {             _callprint(element);         }, 50); }  function _getelementhtmlincludingformelements(element) {     var $element = $(element);     //radiobuttons , checkboxes     $(":checked", $element).each(function () {         this.setattribute('checked', 'checked');     });     //simple text inputs     $("input[type='text']", $element).each(function () {         this.setattribute('value', $(this).val());     });     $("select", $element).each(function () {         var $select = $(this);         $("option", $select).each(function () {             if ($select.val() == $(this).val())                 this.setattribute('selected', 'selected');         });     });     $("textarea", $element).each(function () {         //thanks http://blog.ekini.net/2009/02/24/jquery-getting-the-latest-textvalue-inside-a-textarea/         var value = $(this).attr('value');         //fix issue 7 (http://plugins.jquery.com/node/13503 , http://github.com/erikzaadi/jqueryplugins/issues#issue/7)         if ($.browser.mozilla && this.firstchild)             this.firstchild.textcontent = value;         else             this.innerhtml = value;     });     //http://dbj.org/dbj/?p=91     var elementhtml = $('<div></div>').append($element.clone()).html();     return elementhtml; }  function _getbasehref() {     var port = (window.location.port) ? ':' + window.location.port : '';     return window.location.protocol + '//' + window.location.hostname + port + window.location.pathname; }  function _getmarkup(element, opts) {     var $element = $(element);     var elementhtml = _getelementhtmlincludingformelements(element);      var html = new array();     html.push('<html><head><title>' + opts["pagetitle"] + '</title>');     if (opts["overrideelementcss"]) {         if (opts["overrideelementcss"].length > 0) {             (var x = 0; x < opts["overrideelementcss"].length; x++) {                 var current = opts["overrideelementcss"][x];                 if (typeof (current) == 'string')                     html.push('<link type="text/css" rel="stylesheet" href="' + current + '" >');                 else                     html.push('<link type="text/css" rel="stylesheet" href="' + current["href"] + '" media="' + current["media"] + '" >');             }         }     }     else {         $("link", document).filter(function () {             return $(this).attr("rel").tolowercase() == "stylesheet";         }).each(function () {             html.push('<link type="text/css" rel="stylesheet" href="' + $(this).attr("href") + '" media="' + $(this).attr('media') + '" >');         });     }     //ensure relative links work     html.push('<base href="' + _getbasehref() + '" />');     html.push('</head><body style="' + opts["printbodyoptions"]["styletoadd"] + '" class="' + opts["printbodyoptions"]["classnametoadd"] + '">');     html.push('<div class="' + $element.attr('class') + '">' + elementhtml + '</div>');     html.push('<script type="text/javascript">function printpage(){focus();print();' + ((!$.browser.opera && !opts["leaveopen"] && opts["printmode"].tolowercase() == 'popup') ? 'close();' : '') + '}</script>');     html.push('</body></html>');      return html.join(''); }; })(window); 

i've tried bit of code:

$('a[href="#print"]').bind( "click", function() { $("body").printelement(); }); 

this:

$('a[href="#print"]').unbind().bind( "click", function() { $("body").printelement(); }); 

this:

 $('a[href="#print"]').unbind("click").bind( "click", function() { $("body").printelement(); }); 

this:

  $('a[href="#print"]').click(function(e){       e.stoppropagation();       $("body").printelement();    }); 

this:

  $('a[href="#print"]').click(function(e){       e.stopimmediatepropagation();       $("body").printelement();    }); 

this:

$('a[href="#print"]').on( "click", function() { $("body").printelement(); }); 

and i've thrown in stoppropgation method in bind methods in various combinations. continue work first time , fire multiple times afterwards.

this complete guess worth trying: code inside body?

$('a[href="#print"]').click(function(){$("body").printelement();}); 

the plugin copies content body (in case) , reproduces in iframe, might evaluating event binding every time plugin runs, creating loop. try moving code above outside area printing (body).


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -