最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Can I wait for location.href to load a page? - Stack Overflow

programmeradmin7浏览0评论

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?

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?

Share Improve this question asked Sep 30, 2011 at 14:59 PaulPaul 11.8k32 gold badges92 silver badges145 bronze badges 1
  • location.href = "/search.php"; will immediately redirect the browser, and any code after that won't run (or may run until the new page is loaded). – gen_Eric Commented Sep 30, 2011 at 15:04
Add a ment  | 

3 Answers 3

Reset to default 6

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

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.

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.

发布评论

评论列表(0)

  1. 暂无评论