Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to start JS immediately after loading the ”Notifications page”


  • mutoha
    Participant

    @mutoha

    For example,
    This code will be launched in before loading “Profile Page”.

    function buddypress_profile_layout() {
    	global $bp;
    	if (BP_XPROFILE_SLUG != $bp->current_component) return false;
    	wp_enqueue_script('test', 'http://example.com/wp-content/themes/twentyfifteen-child/js/test.js', array('jquery'), '1.0', true);
    }
    add_filter('wp_enqueue_scripts', 'buddypress_profile_layout');

    But,
    I want a code that launched in after loading “Notifications page”.

    Would you mind teaching me how to do that?

    Thanks.

Viewing 11 replies - 1 through 11 (of 11 total)

  • Prashant Singh
    Participant

    @prashantvatsh

    Hi,

    You can put code like this:

    function buddypress_notifications_layout() {
    	global $bp;
    	if ('notifications'== $bp->current_component){
                  wp_enqueue_script('test', 'http://example.com/wp-content/themes/twentyfifteen- 
                  child/js/test.js', array('jquery'), '1.0', true);
            }
    }
    add_action('wp_enqueue_scripts', 'buddypress_notifications_layout');

    Thanks


    mutoha
    Participant

    @mutoha

    Thank you very much for your quick reply!

    But what I want to know is “after loading”.

    I want to start JS immediately after the message “Loading notifications. Plase wait.”.


    Prashant Singh
    Participant

    @prashantvatsh

    You can check buddypress-notifications.js file and I think you need to add new listener there. Then only you can run your script just after that, but editing a core file is never recommended.

    Thanks


    mutoha
    Participant

    @mutoha

    I see!Thanks!

    If I want to display alert('completed!'), where do I write that?

    Even if I wrote alert at the end of that, it was displayed before loading Notifications List.


    Prashant Singh
    Participant

    @prashantvatsh

    Please try in different functions like start: function() {

    Thanks


    mutoha
    Participant

    @mutoha

    Sorry, What is start: function() {?

    Does it mean the following?

    function() {
     alert('completed!');
    });

    If it’s so, Where dose I write that?

    Thanks.


    Prashant Singh
    Participant

    @prashantvatsh

    No, If you will check there is a function already written:

    start: function() {
    			this.setupGlobals();
    
    			// Listen to events ("Add hooks!")
    			this.addListeners();
    		},

    just find this and in this function write alert('completed!');

    Thanks


    mutoha
    Participant

    @mutoha

    Umm…It’s difficult.

    This time I solved it as follows.

    (function($){
    
      var url = location.href	
      var urlSplit_notifications = url.split('/')[5];	
    	
      if( urlSplit_notifications == 'notifications' ){
    
        const target = document.getElementById("notifications-user-list");
    
        const observer = new MutationObserver(records => {
          alert('completed!');
        });
    
        const options = {
          childList: true
        };
    
        observer.observe(target, options);
    
        let shouldStopObserving = false;
        if(shouldStopObserving){
          observer.disconnect();
        }	
    		
      }
    	
    })(jQuery);	

    I really appreciate your help in resolving the problem.
    See you whenever.


    Prashant Singh
    Participant

    @prashantvatsh

    Perfect 🙂


    mutoha
    Participant

    @mutoha

    Thanks a lot:)
    But there is something strange.

    In case of “activity” it can not be acquired this way.

    If it’s not too much trouble, give me a reply please.
    https://buddypress.org/support/topic/how-to-change-design-only-for-specific-activities/

    Thanks.


    Prashant Singh
    Participant

    @prashantvatsh

    We can mark it resolved now 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.
Skip to toolbar