tags:

views:

81

answers:

3

I have an AJAX call to load a function once the page is loaded, the function opens a lightbox a like frame with some data. The issue is that if I click on close (lightbox frame), the page loads again and again loading the frame so the use never reaches the page under the layer of frame since the frame load on infinite loop. The Ajax is repeating calling the function I guess but I would like to load the frame once and when the user clicks X (close) he may return to the original page.

$(document).ready(function(){
var city = $('#citycode').html();

$.ajax({
//when page is loaded, fire the following function
success: function(){
//the function to be fired located in the page in seperate file
openX(city + //some other parameters);
}});


});

Any tips?

+4  A: 

You can set a global variable on top of the page and set it to true once the page was loaded then later in your code you check if the variable is false load the page not other wise. Here is the prototype:

<script type="text/javascript">

var loaded = false;

$(document).ready(function(){
var city = $('#citycode').html();

$.ajax({
//when page is loaded, fire the following function
success: function(){
if (loaded === false)
{
  //the function to be fired located in the page in seperate file
  openX(city + //some other parameters);
  loaded = true;
}
}});


});
</script>
Sarfraz
Thanks a lot I think this might be the best solution, however I'm not sure if this will work 100 % since we have the Close link on the frame itself and not the page. Do we have to take care of onclick for that link somehow?
digitup
@digitup: The condition is checking that internally already.
Sarfraz
@Sarfraz;I have changed and tried your code but still get the infinite loop once I click on Close button. Any other tips?
digitup
A: 

A random "cache" parameter may be being added by jQuery to inhibit caching. Try adding setting the cache property to true in the ajax call to allow caching; your code may look like this:

$.ajax({
  cache: false;
  success: function(...){...},
  ...
});
Delan Azabani
Thanks alot, since we load real time data, the cache might cause incorrect results!! I don't know I will have to try your approach. Thanks for your answer
digitup
+1  A: 

I've interpreted your question in a different way to the other answers, so here goes... :)

I'm guessing the Close button is a hyperlink, and the default action (changing the URL of the page), is not being prevented?

If this sounds right, you'll need to prevent the default action by capturing the click event on the link, and calling the preventDefault() method.

Without seeing your code, here's the best example I can give:

$('div.lightbox').live('click', function (event) {
  event.preventDefault();
});

Note the use of live() here.

Matt
thanks a lot, this sounds like a solution but I still want the user to be able to close the opened frame and reach the normal page since the frame is opened above the page content itself.
digitup
Perhaps I didn't explain the use of `preventDefault()`. The default action of clicking an anchor tag is to open the URL in its `href` attribute. The default action of clicking a submit button in a form is to submit said form. Big deal so far. Calling `preventDefault()` will still execute all event handlers that have been bound to the event (i.e closing the frame in question), however will, as the name suggests, prevent the default action being performed; e.g prevent the page reloading.
Matt
Thanks again Matt, where is the best place to place the code that takes care of the link? above my code or after my ajax call has been executed?
digitup
The use of `live()` here means you can insert this code inside your `$(document).ready()` block without problems.
Matt
Thanks Matt, I appreciate your help. I will try and let you know the results asap.
digitup
Matt, I have tried your approache and I am sorry to say it is not working do you have any other hints?
digitup