Use "ajaxurl" in js file

Hello everybody!

I make wp theme. I need to know in the js file, if the admin part of the site is open. Is it possible to use this global variable “ajaxurl” for it?


if (typeof ajaxurl === "undefined") {
    console.log ( "You are in frontend of site");
else {
    console.log ( "You are in backendof site");

Are you asking if you can access ajaxurl on both the frontend and admin area?

I would just check if body.hasClass(‘wp-admin’)

Thanks for answer. No I read that “ajaxurl” exist only in admin part and I think to use it for test:
-if "ajaxurl " exist - it mean that it is admin part of site
-if "ajaxurl " not exist - it mean that it is frontend of site

I read that “ajaxurl” exist only in admin

This is correct but it’s common for other plugins to load it on the frontend so this is not going to be a reliable check for you. @tommusrhodus has a great suggestion.

Thanks, I will read about “.hasClass(‘wp-admin’)” more now