Paul
Paul

Reputation: 11746

Can I wait for location.href to load a page?

Is it possible to know when a page has loaded using location.href? My current code looks like this and I want to trigger some events to happen once my page has finished loading. I tried using $.get but it breaks my current implementation of jquery address so this won't work.

$('.search').click(function(){
    location.href = "/search.php"; 
    // trigger event here...
    return false;
});

This will NOT work for my current setup:

$.get('search.php', function() {
    alert('Page has finished loading');
});

Are there any other options?

Upvotes: 0

Views: 3359

Answers (3)

DJ Quimby
DJ Quimby

Reputation: 3699

Have you tried this:

$(document).ready(function () {
  alert('Page has finished loading');
});

EDIT: Removed second portion of answer since the purposeful redirect negates it's usefullness

Upvotes: 6

Jacob
Jacob

Reputation: 78840

Once you change your location, your page unloads. You cannot run script after doing so. All you can do is detect when the new page has loaded within the new page as @DJ Quimby's answer describes.

An exception is if you change your hash location but stay within the same document. That can be bound to an event using jQuery.

Upvotes: 2

Brian
Brian

Reputation: 8616

That will simple redirect the browser...

You want to use...

$.ajax({ ... });

Call using jQuery to call search.php and you can then load the results into a div or something.

Upvotes: 1

Related Questions