JavaScript How to have 1 jQuery file with functions used on different pages?

So I am trying to keep all my functions in 1 jQuery file. Still learning jQuery btw

However I'm running into problems where a div id doesn't exist on a different page and it throws and error. I use the Pop Easy modal plugin. I call $('.modalLink').modal() to initialize the plugin. It works on my home page, but will raise error on my FAQ page because there is no div with modalLink class.

Object has no method 'modal'

I don't want to create separate files, so how would you re-write this?

$(document).ready(function () {

    // PopEasy on Home Page
    $('.modalLink').modal({
        trigger: '.modalLink',          // id or class of link or button to trigger modal
        olay:'div.overlay',             // id or class of overlay
        modals:'div.modal',             // id or class of modal
        animationEffect: 'fadeIn',      // overlay effect | slideDown or fadeIn | 
        videoClass:'video',             // class of video element(s)
        close:'.closeBtn'               // id or class of close button
    });

    /*... other code for other pages go below  ....*/

    // Register Page
    $('.login-window input').on('click focusin', function() {
        this.value =  '';
    });

    // FAQ Page
    //  FAQ sidebar menu
    $('#faq-btn-general').click(function() {
        $(this).parent().addClass('faq-blue');
        $('#img-general-on').show();
        $('#img-general-off').hide();
        $('#dl-general').fadeToggle("slow", "linear");
    });

});
Answer:1

If there is no method .modal(), then that is probably because you don't have the proper library loaded to support the .modal() method.

For a well behaved jQuery method (which I assume .modal() is), there should be no problem with:

$('.modalLink').modal();

if the .modalLink object doesn't exist. The $('.modalLink') jQuery object will be empty and calling .modal() on it will just do nothing (or that's how jQuery methods are supposed to be written anyway). If the .modal() method is not well behaved, then you can protect it yourself with this:

// makes sure the .modalLink object exists and that the `.modal()` method exists
var modalObj = $('.modalLink');
if (modalObj.length && modalObj.modal) {
    modalObj.modal(...);
}
Answer:2

I would recommend checking to see if the objects exist before adding events to them

You can do so with this.

if ( $('#myDiv').length ){}

So to replace some of your code it would look like this

if ( $('.login-window input').length ){
    $('.login-window input').on('click focusin', function() {
        this.value =  '';
    });
}
Answer:3

I've got a template with two kind of article-container: Viewer and Editor: <article ng-if="!editor" ng-controller="article"> <div>Some content</div> </article> <...

I've got a template with two kind of article-container: Viewer and Editor: <article ng-if="!editor" ng-controller="article"> <div>Some content</div> </article> <...

JS FIDDLE I have a function as follows in javascript function myfun() { var a = "'\u0c39"; alert(a); } When i called this function i am able to see correct letter(This letter is telugu ...

JS FIDDLE I have a function as follows in javascript function myfun() { var a = "'\u0c39"; alert(a); } When i called this function i am able to see correct letter(This letter is telugu ...

  1. unable to convert unicode to string

[updated] given object: var node = { 'comments': 'http://example.com/post/#comments', 'slash:comments': '143', } One can access the 'title' parameter using indexed access or dot notation: node["...

[updated] given object: var node = { 'comments': 'http://example.com/post/#comments', 'slash:comments': '143', } One can access the 'title' parameter using indexed access or dot notation: node["...

Ok guys. I've built a web application with some friends that is somehow complex and took us some time to built. It is entirely built using JavaScript Now the application has some functions which we ...

Ok guys. I've built a web application with some friends that is somehow complex and took us some time to built. It is entirely built using JavaScript Now the application has some functions which we ...

  1. server side javascript frameworks
  2. server side javascript marketing cloud
  3. server side javascript example
  4. server side javascript injection
  5. server side javascript tutorial
  6. server side javascript vs client side
  7. server side javascript console.log
  8. server side javascript w3schools
  9. server side javascript object
  10. server side javascript injection payloads
  11. server side javascript code injection
  12. server side javascript vs php
  13. server side javascript libraries
  14. server side javascript code
  15. server side javascript object example
  16. server side javascript is also known as
  17. server side javascript is known as
  18. server side javascript object mcq
  19. server side javascript has stopped working
  20. server side javascript injection example