• Resolved soeezauto

    (@soeezauto)


    Hello

    I have added to functions.php a custom script that should be available to every page.

    Like this:

    function mytheme_custom_scripts(){
        wp_register_script( 'bpopup', '//www.soeezauto.com/js/bpopup_16051.js', array('jquery'),null,true);
        wp_enqueue_script( 'bpopup' );
    }
    
    add_action('wp_enqueue_scripts', 'mytheme_custom_scripts');

    It does load the script and calls it, but gives me a type error the first time it encounters a jquery $ sign, which is a good sign that it does not have the jquery library.

    This is the line that generates the type error:

    var wwdt = $(window).width();

    You can check this at: this link

    I did check around my installation to see if there is any other call to jquery but there isn’t.

    Note that this is a WordPress installation on a “regular” website.

    Appreciate any assistance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter soeezauto

    (@soeezauto)

    Well, it is a bit stranger than that.

    I did some additional testing and included the below code on top of the jquery file:

    jQuery(document).ready(function($) {
        $('body').css('background', 'red');
    });

    That works, but still it hangs on the $(window).width(); line

    WordPress loads jQuery in noConflict mode by default. The typical way to get around this to pass $ as an argument to a function, like you’re doing here. What’s happening, though, is that on line 3, you’ve closed off the function attached to document.ready:

    jQuery(document).ready(function($) {
        $('body').css('background', 'red');
    });

    so the $ shortcut isn’t available to any other jQuery outside that function. You need to wrap the entire script in the function attached to document.ready so the $ shortcut is available at all times.

    Thread Starter soeezauto

    (@soeezauto)

    Thanks Stephen

    It is doing some of the things that are supposed to do, but it still have issues with some functions inside that file.

    Or maybe the issue is with javascript event handlers.

    So, unfortunately my problems did not end up there. It still seems to have issues reading that .js file.

    I have a javascript onclick() event attached to a navigation menu that I am incorporating into WP, that looks like:

    <button id="ad_call" class="btn" onclick="is_signed()" > <i class="fa fa-car"></i> annoncez-le</button>

    The is_signed() function is within the above file ( bpopup_16051.js ), but firebug gives me a reference error, not defined when I click on the button.

    is_signed is :

    function is_signed_account() {
        if (sess === 0) {
            $("#signin_button").click();
            $("#login .account_notice, .signup_link").show();
            $("#login_message").hide();
        } else {
            location.href = '/mon-compte';
        }
    }

    Thanks

    Your script bpopup_16051.js is bit disjointed, so it’s hard to tell exactly what’s wrapped in document.ready() anonymous functions and what isn’t. I’d suggest trying to clean it up a little.

    What’s probably happening here is that is_signed() is defined within the scope of an anonymous function attached to document.ready() and so it isn’t available outside that context, like to an onclick event defined directly in the HTML. Instead, what people typically do is attach it inside jQuery itself:

    $( document ).ready( function() {
      ... other code snipped for clarity ...
    
      function is_signed() {
        ... code goes here ...
      }
    
      $( '#ad_call' ).click( is_signed );
    });
    Thread Starter soeezauto

    (@soeezauto)

    Thanks, again.

    I know the script is not very elegant, but it does work perfectly well. Outside WP, that is.

    It seems clear that I have to remove the javascript event handler in the html and make a jquery setup, attaching to the element.

    I would like to avoid that, as this script is used elsewhere.

    Too bad.

    In any case, thank you for showing me the way.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘jquery do not seem to be loaded’ is closed to new replies.