1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Accessing elements after DOM fired.

Discussion in 'General PHP and MySQL Discussions' started by James Freeman, Apr 19, 2013.

  1. James Freeman

    James Freeman Member

    Hi,

    I am writing some code for the front end of my site. I'm forced to use AJAX as our page loading method. We use anchors as hooks to detect what page the user was on (not sure if that's needed).

    I'm having some problems with a tags after the DOM is loaded. So if you click onto another page which is loaded from AJAX it doesn't take the _initPages(); route.

    Now, I know what you are going to say, just run that function every time I detect a hash change. I don't want to do that.

    What I'm asking is there any jQuery function that looks at the DOM on click instead of at the time of the function is ran. Errr, confusing.

    Thanks,

    Code:

    Code:
    __construct: function()
    {             
               
                this._initPages();
               
                $(window).bind('hashchange', function()
                {
                       
                    mySite._detectHash();
                   
                    if( mySite._hash )
                    { 
                       
                        mySite._loadPage(mySite._hash); 
                       
                    }             
                       
                });
               
                $(window).trigger("hashchange");
               
                this._init();     
               
            },
     
            _initPages: function()
            {
             
             
                $( 'a:not(.noAjax)' ).on( 'click', function( )
                {
             
                    window.location = '/#!/' + $( this ).attr('href');
                   
                    return false;
                   
                } );
               
            }
    
     
  2. Adam Howard

    Adam Howard Well-Known Member

    The recommended method would actually be to use .on() which provides all functionality covering the previous methods (live events, which was replaced) of attaching event handlers.

    http://api.jquery.com/on/

    Documentation is handily provided in the help for converting from the older jQuery event methods .bind(), .delegate(), and .live()
     
  3. James Freeman

    James Freeman Member

    Yeah, I'm using on at the moment as per my code. but it doesn't work :S
     
  4. Adam Howard

    Adam Howard Well-Known Member

    What's the full code? Because what you've displayed isn't the complete thing (to much missing for anyone to help debug it).
     
  5. James Freeman

    James Freeman Member

    I've sent you over a PM, I'm trusting you with sensitive code. We have information in there that we don't want getting out to our competitors.
     
  6. Adam Howard

    Adam Howard Well-Known Member

    I can't be trusted :sneaky:

    .
    .
    .
    .
    .
    .
    .

    (I'm just joking.:p No worries.:) But I bet I made your heart skip a beat there for a moment.:X3: LOL)

    It will take me a while to review the code.
     
    James Freeman likes this.
  7. Adam Howard

    Adam Howard Well-Known Member

    I gave a quick look over and from what I can see.... Nicely done. (y)

    I'm wondering if maybe the conflict lies else where (conflict with something outside your code) or maybe we're both (you and me) missing something here.

    I'd suggest 2 names in who I believe you can trust help debug this issue further (you should get in contact with one of them)
    I also suggest these two because from the code you gave me, its clear this is part of a larger project. And I'm kind of limited on the time it would take to review the whole (larger picture).

    But from what I could see... The code looked clean to me and should be loading smoothly. So clearly we're both missing something.
     
  8. James Freeman

    James Freeman Member

    If you are in direct contact with them could you point them in this direction (give them the code if you need). I'm fairly new around here.
     
    Adam Howard likes this.
  9. Adam Howard

    Adam Howard Well-Known Member

    Will pass on the message (as requested)
     
    James Freeman likes this.
  10. Adam Howard

    Adam Howard Well-Known Member

    James Freeman likes this.

Share This Page