• Micemade

    (@anydog)


    Hi,

    I believe there’s a conflicting JS variable “ajaxurl” – the browser console is reporting following error, in the admin dashboard page and customizer:

    TypeError: ajaxurl.indexOf is not a function

    The code in question is here (just a fragment), showing that it actually belongs to Polylang JS code (the “pll_ajax_backend”):

    $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
    	if ( -1 != options.url.indexOf( ajaxurl ) || -1 != ajaxurl.indexOf( options.url ) ) {
    		if ( 'undefined' === typeof options.data ) {
    			options.data = ( 'get' === options.type.toLowerCase() ) ? 'pll_ajax_backend=1' : {"pll_ajax_backend":1};
    		} else {
    ... the rest of the code
    

    What are yout thoughts about this?
    Can you confirm that this issue exist, and if so, when do you believe it would the fix be available?

    Thanks

    • This topic was modified 4 years ago by Micemade.
    • This topic was modified 4 years ago by Micemade.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Chouby

    (@chouby)

    Hello,

    I don’t reproduce the issue. You should look for a conflict with your theme or a plugin.

    Thread Starter Micemade

    (@anydog)

    Hi and thanks for replying.

    Seemingly, there was a confilct with another plugin (solved and updated today). However, the JS code above clearly is Polylang’s , and the error reported was on that line.
    Since I’m also a plugins (and themes) author may I give a suggestion (I hope you don’t mind) – to avoid such a conflicts I always use wp_localize_scripts for using global js variables in my code. It outputs JS object, which you can prefix to make sure your vars won’t conflict. Example output of wp_localize_scripts:

    var MyPluginJsVars = {
        "ajaxURL":"https:\/\/my-site\/wp-admin\/admin-ajax.php",
        "homeurl":"https:\/\/my-site\/",
        "nested_node":{
            "first_child":"value",
            "second_child":"another_value",
            "empty":""
        }
    };

    So, you know how to use it … Again, just a suggestion and hope you don’t mind.

    Best regards

    • This reply was modified 3 years, 12 months ago by Micemade.
    Plugin Author Chouby

    (@chouby)

    We indeed use wp_localize_script when we want to pass values from PHP to JS. See https://github.com/polylang/polylang/search?q=wp_localize_script&unscoped_q=wp_localize_script

    But I don’t see how it could help here. ajaxurl is a global variable provided by WordPress. You should have it in your admin header.

    Thread Starter Micemade

    (@anydog)

    Ok, that’s great you’re already using wp_localize_script. Thanks for the info and sorry for that. I didn’t checkk your code … :-/
    In the meantime, the conflict seems to be solved, and it was the other plugin’s issue.

    The thing is – browser console reported error exactly on Polylang’s line of code:
    TypeError: ajaxurl.indexOf is not a function
    Isn’t that global var in Polylang’s code?
    The console error log confused me …

    Anyhow, thanks and I apologize if I wasted your time.

    Best regards.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Ajaxurl JS var conflict’ is closed to new replies.