Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder GSimpson's Avatar
    Join Date
    Aug 2006
    Location
    New Zealand
    Posts
    268
    Thanks
    9
    Thanked 0 Times in 0 Posts

    jQuery Load Re-evaluation

    Hi there,

    I've recently started a personal project to recreate a flash website using jquery and some jQuery plugins. All has gone well apart from one thing, I've only found one proposed solution to it and I can't seem to get it work since it's not in the context of jQuery.

    Here's a description of what I've got so far:

    I have a content management system that outputs data to a content div that can contain links, images etc. I have links around this content area that are not inside it that have a class named "ajax-link". This is the same for all of the links inside the content area. Now using the following code, I can get all links that are written by php at load time, to change the content of my content div. (This includes the initial content of the content div)

    Code:
    $("a.ajax-link").click(function(){
    	strTargetPage = $(this).attr("href")+' #content';
    	$("#ajax").html('<div id="content"><span class="loading-message">Loading, Please Wait.</span></div>')
    	.load(strTargetPage, function() { return false; });
    	return false;
    });
    This will load the content div of a links target page into the page I'm currently using. This works 100% for any links that are written by php initially. So I'm guessing that anything with a class of "ajax-link" will have this function assigned to it at jQuery's "ready" time.

    Is there anyway I can have the newly loaded content be re-evaluated so that the new "ajax-link" links will have this function assigned to them?

    I've been told to use javascript's eval() function when I've used standard javascript in the past, but I wouldn't have a slightest clue on how to achieve this in jQuery, as jQuery's load function should be evaluating javascript when it's loaded.

    Any ideas/suggestions?
    If you need to see my php-generated source code, I can provide it.

    Thank you in advance.
    The internet is my Sandbox, and notepad is my Spade n' Bucket.

  • #2
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,949
    Thanks
    9
    Thanked 724 Times in 718 Posts
    Do you mean you are creating new .ajax-link elements via ajax, and you want the same click event applied to them? If so, .live() should do what you want, so your code becomes:

    Code:
    $("a.ajax-link").live('click',function(){...etc

  • Users who have thanked SB65 for this post:

    GSimpson (11-08-2010)

  • #3
    Regular Coder GSimpson's Avatar
    Join Date
    Aug 2006
    Location
    New Zealand
    Posts
    268
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Hey thanks for that.

    Yes it's exactly what I've been after. I just read about hijax and similar techniques so I've been looking for a way to do that. A few minutes ago I found the original plugin "livequery" and installed that. I've got it working, but If I can throw out the extra 5kb and replace it with the live() function you mentioned, then it'll be another improvement.

    Thanks!
    The internet is my Sandbox, and notepad is my Spade n' Bucket.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •