0

I have been with that a whole day.. I have a home page(index.php) and i have a small menu in it(made up of buttons) and a <div id=tab_contents></div>

i have used AJAX in such a way that whenever i click on any of these buttons, another page is loaded in the tab_contents-div.ie:home_tab0.php, home_tab1.php, home_tab2.php for each button respectively.

The page that i want to fetch with ajax should contain a <body onload=initialize()> ...</body> function.or it can contain a javascript code snippet to trigger the initilization() function. that is when the button is clicked,the page lets say home_tab0.php is loaded, codes inside the home_tab0.php should trigger the initialization() frunction.

i have tried every possible way in my knowledge to make it work but without success...:( please if i can get any help for this i would be so grateful.

Pointy
  • 405,095
  • 59
  • 585
  • 614
welou
  • 3
  • 1
  • 5

2 Answers2

1

With jQuery it's easy to call any function after the ajax call has returned, and data is loaded. I guess that's what you want to do. There are a few examples here:

http://api.jquery.com/jQuery.get/

E.g:

$.get('home_tab0.php', function(data) {
  $('#tab_contents').html(data);
  initialize();
});
Carles Barrobés
  • 11,608
  • 5
  • 46
  • 60
  • okay i will have a look at jquery...i actually don't know much about how to use it.. – welou Dec 06 '10 at 17:01
  • you said using jquery it is possible to call any function after the ajax call has returned..but do i have to call the initialize() function within the ajax function?? – welou Dec 06 '10 at 17:03
  • where is this function located? what is its purpose? can you post more details? anyway, the callback you will be executing after your ajax call is successful can include a call to initialize()... – Carles Barrobés Dec 06 '10 at 21:26
  • the ajax function is located in a javascript file which i included in the home page(index.php) and when i click on the buttons x,y,z which should load home_tab0.php,home_tab1.php,home_tab2.php respectively in div-tab_contents by ajax.each pages when being called in the div-tab_contents should call a javascript function initialize()...the problem is: when fetching say home_tab0.php all javascript inside that page is returned as text..it is not executed. – welou Dec 07 '10 at 06:26
  • I would not include javascript in the home_tab*.php pages. I would return only the partial HTML that you will include in your
    . If you need specific "per-tab" javascript, you can load it and run it with getScript: http://api.jquery.com/jQuery.getScript/
    – Carles Barrobés Dec 07 '10 at 15:39
0

This is a common problem. I recommend using jQuery and letting it take care of this for you. Reimplementing what they've done would be a waste of your time.

http://api.jquery.com/load/

$("#tab_contents").load("http://www.foo.com/loadContent");
Mike Ruhlin
  • 3,546
  • 2
  • 21
  • 31
  • I should add that I haven't ever done this with an onload handler set on the body, so not sure if that'll work. But it will work with just a script tag stuck in the page. – Mike Ruhlin Dec 06 '10 at 16:47
  • i have heard that i could use eval()...i have tried looking for it..but every time they mention it in a forum..they are never clear about what to actually look for... – welou Dec 06 '10 at 16:57
  • You would have to search the returned HTML for – Mike Ruhlin Dec 06 '10 at 17:05
  • there's still another problem...the home_tab0.php contains a
    ... the initialization function calls the map_canvas-div to place some contents(a map-google maps) in it...but will the initialization function be able to find the map_canvas-div inside home_tab0.php?
    – welou Dec 06 '10 at 17:14
  • yes. jQuery.load works by: 1) Fetching the URL requested 2) Inserting the raw HTML from the response into the DOM 3) Executing any scripts that were in the requested HTML. So any elements that exist in that file will be accessible to the script as if they were already there in the parent document. – Mike Ruhlin Dec 06 '10 at 17:17
  • i have been reading the jquery link you've sent me...still i have a lot of questions .. i am actually at a lost of how to formulate them..can i come back tomorrow(after figuring out what is jquery) and ask you some more question? – welou Dec 06 '10 at 17:31