Results 1 to 3 of 3

Thread: Javascript in ajax loaded page

  1. #1
    Join Date
    Dec 2009
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Javascript in ajax loaded page

    Hey, my site's setup is like this
    <html>
    <div> content </div>
    <a id="a1"> a </a>
    <script>
    get("a1").onclick = load "response" to content div innerHTML;
    get("a2").onclick = load "a diff response" to content div innerHTML;
    </script>
    </html>

    The file that is loaded is in a sub folder ./contents/sample/index.php
    now in that "sample/index.php" I included something like this
    <a id="a1">click here</a>

    This is not working ..!
    Im using the jquery.load() function to call the ajax page. As far as my knowledge, by loading ajaxed content to innerHTML, css and javascript should work if defined in the main index file and not in the file being fetched.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Use jQuery's live() to assign events to content that might show up later.

    Also make sure that when importing content you are not ending up with more than one element with a given id on the page.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Dec 2009
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Thanks

    Solved it .....
    I was using jQuery load()

    the bug was here in the function declaration

    i declared like this

    window.onload = function(){

    get("link").click = loadAjax;
    function loadAjax(menu){
    $.load()// to get content
    }
    }

    so the file that is loaded was not able to access the function .. So put that out of the onload event handler.
    then it worked...

    Have one more doubt.
    this worked when using jQuery
    the ajax loaded page :
    <a id="aa" onclick="abc()">aaa</a>
    <script>
    function abc(){
    alert("came here");
    }
    </script>

    and this did not work
    in the main page
    <script>
    window.onload = function(){
    $('a').click(clickHandler);
    }
    function clickHandler() {
    $.load(url);
    }
    </script>

    and in the ajaxloaded page
    <a>xxx</a>

Bookmarks

Posting Permissions

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