Guest User

Untitled

a guest
Dec 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.65 KB | None | 0 0
  1. if ($(selector).length > 0) {
  2. // Do something
  3. }
  4.  
  5. if ($(selector).length)
  6.  
  7. jQuery.fn.exists = function(){ return this.length > 0; }
  8.  
  9. if ($(selector).exists()) {
  10. // Do something
  11. }
  12.  
  13. jQuery.fn.exists = function(){return ($(this).length > 0);}
  14. if ($(selector).exists()) { }
  15.  
  16. jQuery.exists = function(selector) {return ($(selector).length > 0);}
  17. if ($.exists(selector)) { }
  18.  
  19. if ( $('#myDiv').length ) { /* Do something */ }
  20.  
  21. if ( $('#myDiv')[0] ) { /* Do something */ }
  22.  
  23. // if element exists
  24. if($('selector').length){ /* do something */ }
  25.  
  26. // if element does not exist
  27. if(!$('selector').length){ /* do something */ }
  28.  
  29. if (document.getElementById('element_id')) {
  30. // Do something
  31. }
  32.  
  33. if ($(selector)[0]) { ... }
  34.  
  35. if ($(selector).is('*')) {
  36. // Do something
  37. }
  38.  
  39. ;;(function($) {
  40. if (!$.exist) {
  41. $.extend({
  42. exist: function() {
  43. var ele, cbmExist, cbmNotExist;
  44. if (arguments.length) {
  45. for (x in arguments) {
  46. switch (typeof arguments[x]) {
  47. case 'function':
  48. if (typeof cbmExist == "undefined") cbmExist = arguments[x];
  49. else cbmNotExist = arguments[x];
  50. break;
  51. case 'object':
  52. if (arguments[x] instanceof jQuery) ele = arguments[x];
  53. else {
  54. var obj = arguments[x];
  55. for (y in obj) {
  56. if (typeof obj[y] == 'function') {
  57. if (typeof cbmExist == "undefined") cbmExist = obj[y];
  58. else cbmNotExist = obj[y];
  59. }
  60. if (typeof obj[y] == 'object' && obj[y] instanceof jQuery) ele = obj[y];
  61. if (typeof obj[y] == 'string') ele = $(obj[y]);
  62. }
  63. }
  64. break;
  65. case 'string':
  66. ele = $(arguments[x]);
  67. break;
  68. }
  69. }
  70. }
  71.  
  72. if (typeof cbmExist == 'function') {
  73. var exist = ele.length > 0 ? true : false;
  74. if (exist) {
  75. return ele.each(function(i) { cbmExist.apply(this, [exist, ele, i]); });
  76. }
  77. else if (typeof cbmNotExist == 'function') {
  78. cbmNotExist.apply(ele, [exist, ele]);
  79. return ele;
  80. }
  81. else {
  82. if (ele.length <= 1) return ele.length > 0 ? true : false;
  83. else return ele.length;
  84. }
  85. }
  86. else {
  87. if (ele.length <= 1) return ele.length > 0 ? true : false;
  88. else return ele.length;
  89. }
  90.  
  91. return false;
  92. }
  93. });
  94. $.fn.extend({
  95. exist: function() {
  96. var args = [$(this)];
  97. if (arguments.length) for (x in arguments) args.push(arguments[x]);
  98. return $.exist.apply($, args);
  99. }
  100. });
  101. }
  102. })(jQuery);
  103.  
  104. if ($.exist('#eleID')) { /* DO WORK */ } // param as STRING
  105. if ($.exist($('#eleID'))) { /* DO WORK */ } // param as jQuery OBJECT
  106. if ($('#eleID').exist()) { /* DO WORK */ } // enduced on jQuery OBJECT
  107.  
  108. $.exist('#eleID', function() { // param is STRING && CALLBACK METHOD
  109. /* DO WORK */
  110. /* This will ONLY fire if the element EXIST */
  111. }, function() { // param is STRING && CALLBACK METHOD
  112. /* DO WORK */
  113. /* This will ONLY fire if the element DOES NOT EXIST */
  114. })
  115.  
  116. $('#eleID').exist(function() { // enduced on jQuery OBJECT with CALLBACK METHOD
  117. /* DO WORK */
  118. /* This will ONLY fire if the element EXIST */
  119. })
  120.  
  121. $.exist({ // param is OBJECT containing 2 key|value pairs: element = STRING, callback = METHOD
  122. element: '#eleID',
  123. callback: function() {
  124. /* DO WORK */
  125. /* This will ONLY fire if the element EXIST */
  126. }
  127. })
  128.  
  129. $.fn.exists = $.fn.exists || function() {
  130. return !!(this.length && (this[0] instanceof HTMLDocument || this[0] instanceof HTMLElement));
  131. }
  132.  
  133. $(1980).exists(); //return false
  134. $([1,2,3]).exists(); //return false
  135. $({name: 'stackoverflow', url: 'http://www.stackoverflow.com'}).exists(); //return false
  136. $([{nodeName: 'foo'}]).exists() // returns false
  137. $('div').exists(); //return true
  138. $('.header').exists(); //return true
  139. $(document).exists(); //return true
  140. $('body').exists(); //return true
  141.  
  142. if(document.getElementById("myElement")) {
  143. //Do something...
  144. }
  145.  
  146. jQuery.fn.extend({
  147. exists: function() { return this.length }
  148. });
  149.  
  150. if($(selector).exists()){/*do something*/}
  151.  
  152. function elementIfExists(selector){ //named this way on purpose, see below
  153. return document.querySelector(selector);
  154. }
  155. /* usage: */
  156. var myelement = elementIfExists("#myid") || myfallbackelement;
  157.  
  158. var myel=elementIfExists("#myid");
  159. // now we are using a reference to the element which will linger after removal
  160. myel.getParentNode.removeChild(myel);
  161. console.log(elementIfExists("#myid")); /* null */
  162. console.log(myel); /* giant table lingering around detached from document */
  163. myel=null; /* now it can be garbage collected */
  164.  
  165. /* locally scoped myel gets garbage collected even with the break; */
  166. for (var myel; myel = elementIfExist(sel); myel.getParentNode.removeChild(myel))
  167. if (myel == myblacklistedel) break;
  168.  
  169. function elementExists(selector){
  170. return !!document.querySelector(selector);
  171. }
  172. /* usage: */
  173. var hastables = elementExists("table"); /* will be true or false */
  174. if (hastables){
  175. /* insert css style sheet for our pretty tables */
  176. }
  177. setTimeOut(function (){if (hastables && !elementExists("#mytablecss"))
  178. alert("bad table layouts");},3000);
  179.  
  180. var $target = $({});
  181. console.log($target, $target.length);
  182.  
  183. // Console output:
  184. // -------------------------------------
  185. // [▼ Object ] 1
  186. // ► __proto__: Object
  187.  
  188. if ($.isEmptyObject(selector) || !$(selector).length) {
  189. throw new Error('Unable to work with the given selector.');
  190. }
  191.  
  192. $.isEmptyObject('hello') // FALSE in Chrome and TRUE in IE
  193.  
  194. // These by Id
  195. if( $('#elementid').length > 0){
  196. // Element is Present
  197. }else{
  198. // Element is not Present
  199. }
  200.  
  201. // These by Class
  202. if( $('.elementClass').length > 0){
  203. // Element is Present
  204. }else{
  205. // Element is not Present
  206. }
  207.  
  208. $(selector).length && //Do something
  209.  
  210. if ($("#myDiv").length) {
  211. $("#myDiv").show();
  212. }
  213.  
  214. $("#myDiv").show();
  215.  
  216. jQuery.fn.exists = function(){return !!this.length};
  217.  
  218. if ($(selector).exists()) {
  219. // the element exists, now what?...
  220. }
  221.  
  222. if (!!$(selector)[0]) // do stuff
  223.  
  224. $.fn.exists = function() {
  225. return $.contains( document.documentElement, this[0] );
  226. }
  227.  
  228. function isExists(selector){
  229. return document.querySelectorAll(selector).length>0;
  230. }
  231.  
  232. // Checks if an object exists.
  233. // Usage:
  234. //
  235. // $(selector).exists()
  236. //
  237. // Or:
  238. //
  239. // $(selector).exists(anotherSelector);
  240. jQuery.fn.exists = function(selector) {
  241. return selector ? this.find(selector).length : this.length;
  242. };
  243.  
  244. $.fn.exists = function(callback) {
  245. var self = this;
  246. var wrapper = (function(){
  247. function notExists () {}
  248.  
  249. notExists.prototype.otherwise = function(fallback){
  250. if (!self.length) {
  251. fallback.call();
  252. }
  253. };
  254.  
  255. return new notExists;
  256. })();
  257.  
  258. if(self.length) {
  259. callback.call();
  260. }
  261.  
  262. return wrapper;
  263. }
  264.  
  265. $("#elem").exists(function(){
  266. alert ("it exists");
  267. }).otherwise(function(){
  268. alert ("it doesn't exist");
  269. });
  270.  
  271. $.fn.exists = (callback) ->
  272. exists = @length
  273. callback.call() if exists
  274. new class
  275. otherwise: (fallback) ->
  276. fallback.call() if not exists
  277.  
  278. if(document.querySelector('.a-class')) {
  279. // do something
  280. }
  281.  
  282. function exists(selector) {
  283. return $(selector).length;
  284. }
  285.  
  286. if (exists(selector)) {
  287. // do something
  288. }
  289.  
  290. $.fn.ifExists = function(fn) {
  291. if (this.length) {
  292. $(fn(this));
  293. }
  294. };
  295. $("#element").ifExists(
  296. function($this){
  297. $this.addClass('someClass').animate({marginTop:20},function(){alert('ok')});
  298. }
  299. );
  300.  
  301. $.fn.exist = function(callback) {
  302. return $(this).each(function () {
  303. var target = $(this);
  304.  
  305. if (this.length > 0 && typeof callback === 'function') {
  306. callback.call(target);
  307. }
  308. });
  309. };
  310.  
  311. $.fn.exist = function(onExist, onNotExist) {
  312. return $(this).each(function() {
  313. var target = $(this);
  314.  
  315. if (this.length > 0) {
  316. if (typeof onExist === 'function') {
  317. onExist.call(target);
  318. }
  319. } else {
  320. if (typeof onNotExist === 'function') {
  321. onNotExist.call(target);
  322. }
  323. }
  324. });
  325. };
  326.  
  327. $('#foo .bar').exist(
  328. function () {
  329. // Stuff when '#foo .bar' exists
  330. },
  331. function () {
  332. // Stuff when '#foo .bar' does not exist
  333. }
  334. );
  335.  
  336. if($("selector").length){
  337. //code in the case
  338. }
  339.  
  340. if ( $('#myDiv').size() > 0 ) { //do something }
  341.  
  342. if($(selector).length){
  343. // true if length is not 0
  344. } else {
  345. // false if length is 0
  346. }
Add Comment
Please, Sign In to add comment