jQuery: Onload Scroll to Object
I've got a bit of a problem with scrolling at the moment. I'm developing a forum app for Facebook and I'm trying to get the page to scroll to the latest post on a page if t开发者_开发问答he user wishes. I can do this no problem when I load the topic via AJAX in the same page, but I run into a problem if the user wants to open the topic in a new page first.
My problem is that I can't get the code to wait until the document finishes loading before it executes (so the page isn't fully loaded and the function fails). I know EXACTLY what you're thinking at this point, and neither $(document).ready()
nor $(window).ready()
are working. The problem is in the fact that this is a Facebook application and I don't have access to the parent frame (as all apps are loaded through an iframe). Luckily I can run the function after an AJAX call and it works fine (seeing as I can wait until the content is fully loaded); however, I've definitely got a problem with opening topics in new tabs.
Regardless, there must be a way to tell when my frame has completely loaded so that I can then initiate the function to scroll to an object. Can anyone give me a few ideas to try out?
I figured out the problem. I generally load the Facebook JS SDK asynchronously and that was causing a delay in being able to use the functions. I'd call them but they weren't loaded yet. I made it load synchronously and the problem is resolved.
The cross-domain stuff makes this awkward. A hackish solution would to include a script block at the very bottom of your iframe content that fires the scrolling:
<script type="text/javascript">
scrollToPost();
</script>
As this script is the last thing to load and execute, you'll not have the issue you had above.
You might also try playing with the iframe.onload handler for a cleaner solution, but I think you'll come up against cross-domain issues again.
精彩评论