I'm working on a multiselect plugin that uses only live events, and the way it works is the select element gets replaced by an input and div element. I would like to supply a callback function where the callback is tied to the input element. Is using the "data" method to store the callback the best way to proceed? Also, how should I handle the removal of the select element. Like this? $.fn.myPlugin = function(options,callback) { this.each(function() { }).remove(); } or do I need to do this: mySelect.myPlugin().remove(); Thanks
Is there a general rule of thumb to avoid orphan nodes? I'm using a multiselect plugin which I'd like to be able to remove from the DOM but it is creating orphan nodes in IE and the nodes are not removed according to sIEve. I suspect it is the order that the elements are removed from the DOM, but I'm not sure. Here is the example I'm working from: <html> <head> <script src='jquery.js'></script> <script src="jqueryMultiSelect.js"></script> <link href="jqueryMultiSelect.css" rel="stylesheet" type="text/css" /> <script> function Add(){ $('#selectBin').html('<select id="control_1" name="control_1[]" multiple="multiple" size="5">'+ '<option value=""></option><option value="option_1">Option 1</option>'+ '<option value="option_2">Option 2</option>'+ '<option value="option_3">Option 3</option>'+ '<option value="option_4">Option 4</option>'+ '<option value="option_5">Option 5</option>'+ '</select>'); $('#selectBin').find('select').multiSelect(); } function Remove(){ $('#selectBin').empty(); } </script> </head> <body> <button onclick='Add()'>Add Select</button> <button onclick='Remove()'>Remove Select</button> <table> <tr> <td id='selectBin'> </td> </tr> </table> </body> </html>
This was supposively fixed in change Rev [2011], but both this rev and the nightly Rev give pseudo leaks (memory usage always goes up). I was able to get a stable "InUse" DOM elements within sIEve by using the discardElement method (see http://www.outofhanwell.com/ieleak/index.php?title=Fixing_Leaks), but the memory usage continues to climb regardless. Here is the link with the sample I'm testing against: http://dev.jquery.com/ticket/1233
I'm playing around with writing a server-side script that generates JSONP from content that is downloaded by the script (URL is passed to script from querystring). Is there a better way to do it than to encode it as base64, or is there a work-around that doesn't require any server-side code? I'm basically trying to get access to a given page's DOM cross-domain using minimal server-side code. I suppose one thing that would be needed here is a regex to replace non http src attributes. Here is what I have so far: //test.htm <body> <div id="mydiv"></div> <script src="jquery.js"></script> <script src="jquery.base64.js"></script> <script> function callback(e){ alert(e.data); alert($.base64Decode(e.data)); $("#mydiv").html($.base64Decode(e.data)); } </script> <script src="test.aspx?url=www.wikipedia.org"></script> </body> //test.aspx <%@ Page Language="C#" AutoEventWireup="true" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Net" %> <%@ Import Namespace="System.Text" %> <script runat="server"> void Page_Load( object sender, EventArgs e ){ string url = Request["url"] ?? ""; Response.Write("callback({data:\""+EncodeTo64(Get(url))+"\"})"); Response.End(); } string EncodeTo64(string toEncode){ byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes (toEncode); string returnValue = System.Convert.ToBase64String (toEncodeAsBytes); return returnValue; } string Get(string strURL){ WebRequest myWebRequest = WebRequest.Create("http://"+strURL); WebResponse myWebResponse = myWebRequest.GetResponse(); Stream ReceiveStream = myWebResponse.GetResponseStream(); Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); StreamReader readStream = new StreamReader( ReceiveStream, encode ); return readStream.ReadToEnd(); } </script>
I wrote a quick plugin to replace alert (I didn't like that I couldn't copy and paste from the alert window, and that it was modal). I decided to use window.open instead of absolute divs to show it. I've also used toJSON plugin for when an object is passed in. Faced a couple of problems with getting outerWidth of the browser in IE, and using scrollWidth/scrollHeight to determine window size, which I'm unsure I solved right. feedback? <body> <script src="jquery.js"></script> <script src="json.js"></script> <script> (function($){ $.alert = function(obj,options) { var defaults = { maxWidth: 800, maxHeight: 1000 }; var options = $.extend(defaults, options); w = window.open('','','width=100,height=100,resizable=1'); if(typeof obj == "object"){ obj = "\n$.toJSON("+obj+"):\n" + $.toJSON(obj); } w.document.write("<body topmargin=0 leftmargin=0 style='padding: 0px;height:100%'><textarea id='ta1' wrap='off' style='background- color:#c0c0c0;color:black;font:8pt lucida console;width:100%;height: 100%'>"+obj+"</textarea></body>"); var innerWidth = w.innerWidth || w.document.documentElement.clientWidth || w.document.body.clientWidth; var innerHeight = w.innerHeight || w.document.documentElement.clientHeight || w.document.body.clientHeight; var outerWidth = w.outerWidth+12 || innerWidth+32; var outerHeight = w.outerHeight+12 || innerHeight+51; var ta = $('#ta1',w.document) var scrollX = ta[0].scrollWidth+(outerWidth-innerWidth); var scrollY = ta[0].scrollHeight+(outerHeight-innerHeight); w.resizeTo((scrollX>options.maxWidth)?options.maxWidth:scrollX, (scrollY>options.maxHeight)?options.maxHeight:scrollY); return this; //if returning something else use "return this.pushStack (somethingElse)" so that .end() returns this }; })(jQuery); $.alert(jQuery.data); var obj = {one:"1",two:"2",three:"3",four:function(){test=1}}; $.alert(obj); </script> </body>
Is there a way to determine a drop container without looping through each container and comparing absolute positions? This is the only solution mentioned in this article: http://www.webreference.com/programming/javascript/mk/column2/2.html I don't want to have to move the item being dragged out of the way of the cursor so that i have event.target available. I'm thinking it can be looked up some how with a hash table so that it is O(1). I suppose we can assume that the containers do not overlap.. I suppose if I store for containers i in (0,N)..: container[i].x0 //top left point container[i].y0 container[i].x1 //bottom right point container[i].y1 then I generate javascript like this perhaps: switch(pos){ case pos.x>container0x0&&pos.y>container0x0&&pos.x<container0x1&&pos.y<container0y1: return container[0]; ..etc } ..but then I would have to eval this, and I'm not sure if there would be any performance gain..would the switch generate a hash table? Also, would container[0] even be accessible inside an eval. Is there a way to map the position values to a hashtable for all containers without looping?