English 中文(简体)
How to use arrow keys to reload new URl in browser
原标题:

Facebook and myspace both have a nice feature in the photo galleries, when you go to an image page, a page with the user s image and user comments on the page. You can hit you keyboard left and right arrow key and the page will load the next page, it does it super fast, if you held down the key it would go fast through many pages. I know this is mostly with javascript, my main question is how to make the new pages load all soo quickly?

Im using PHP/mysql/Javascript

最佳回答

You d capture the keys the user is pressing, and response for left-arrows and right-arrows. Capturing keys isn t all that difficult:

$(window).bind("keypress", function(e) {
  var code = (e.keyCode ? e.keyCode : e.which); // which code?
  alert(String.fromCharCode(code)); // which key, according to the code?
});

You d want to adapt that code to your liking. If the right arrow was click, fire off a request to get-image.php?direction=next&user=12838203 and if it were the back-arrow, simply swap out "next" for "prev".

$("#loading").show(); // fancy ajax loading animation
$.post("get-image.php", {direction:"next",user:12838203}, function(response){
  // hide our ajax loading animation
  $("#loading").hide();
  // if we get a proper response, like a source for a new image back
  $("#image").fadeOut("fast").attr("src", response).fadeIn("fast");
});
问题回答

They are not actually loading pages, but only parts of it using Ajax. That s all there is to it - they are loading the images on the fly as you press the buttons. You can watch that in Facebook with an open Firebug in the "Net" tab.

If you re into JQuery, the JQuery Ajax page is a good starting point.

You could look into the source code of one of the popular lightbox scripts for real-world examples.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签