Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to change this JQuery code to check for element existence prior to processing [duplicate]

I have the following code:

            <script type="text/javascript">             $(function () {                 $('#js-news').ticker({                     speed: 0.10,                     htmlFeed: false,                     fadeInSpeed: 600,                     titleText: 'DASHBOARD ALERTS',                     direction: 'ltr',                     displayType: 'reveal',                     controls: false                 });             });         </script> 

I would like to alter it such that it only fires if #js-news is present. In older IE V10 browsers I am getting the error: "Element does not exist in DOM", and the relevant pages do not have the js-news element. Newer browser seem to cope, but not IE7,8,9.

Thoughts on code alteration appreciated.

Thanks.

like image 508
SamJolly Avatar asked May 07 '14 16:05

SamJolly


People also ask

What is $$ in jQuery?

$ sign is just a valid javascript identifier which is used as an alias for jQuery. Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function). Most of them also have a way to relinquish the $ so that it can be used with another library that uses it.

How do you check button is exist or not in jQuery?

Answer: Use the jQuery . length Property You can use the jQuery . length property to determine whether an element exists or not in case if you want to fire some event only if a particular element exists in DOM. Here's an example that displays an alert on button click if the specified element exists.

How check textbox ID exist or not in jQuery?

In jQuery, you can use the . length property to check if an element exists. if the element exists, the length property will return the total number of the matched elements. To check if an element which has an id of “div1” exists.

What does jQuery find return?

jQuery find() Method The find() method returns descendant elements of the selected element. A descendant is a child, grandchild, great-grandchild, and so on. The DOM tree: This method traverse downwards along descendants of DOM elements, all the way down to the last descendant.


2 Answers

First, as @Archer pointed out in the comments above, this plugin is not coded properly. The idiomatic jQuery plugin will fail silently when its method is invoked on a jQuery object with no elements.

As seen from the code here, this plugin either runs the console.log() or an alert() if the console doesn't exist when there's no element:

if ($(this).length == 0) {     if (window.console && window.console.log) {         window.console.log('Element does not exist in DOM!');     } else {         alert('Element does not exist in DOM!');         }     return false; } 

This is pretty bad. The plugin does have a debugMode that can be disabled, but it wouldn't help because that flag isn't used here. To modify the source, you could change the first line above:

if (opts.debugMode && $(this).length == 0) { 

And then be sure to set debugMode:false in your settings.

--

But how this ultimately affects IE is that it doesn't have a console object defined when the dev tools are closed (and never in IE6/7), so a simple solution would be to make sure that object is defined with a NO-OP function.

if (!window.console) {     window.console = {         log: function() {}     }; } 

Now it will find the console and invoke the empty function in IE instead of firing the alert().

There are other methods on the console object that should probably be filled in too, but the log() is the most common.


If you're so inclined, it may not be a bad idea to file a bug report with the developer. It seems that the project hasn't been updated in the last 2 years, so I don't know if it'll help, but it couldn't hurt.

like image 62
cookie monster Avatar answered Oct 12 '22 10:10

cookie monster


Add the condition to check the length:

if($('#js-news').length){   $('#js-news').ticker({                 speed: 0.10,                 htmlFeed: false,                 fadeInSpeed: 600,                 titleText: 'DASHBOARD ALERTS',                 direction: 'ltr',                 displayType: 'reveal',                 controls: false   }); } 
like image 42
Milind Anantwar Avatar answered Oct 12 '22 10:10

Milind Anantwar