开发者

Using AJAX for page items while still allowing them to be opened in a new tab/window

I'm looking at using AJAX to allow some content within part of a page to be reloaded without reloading the entire web page (eg things like overview, reviews, specifications, etc pages about a single item).

The problem开发者_如何学Python is however I still want to allow users to open these items in a new tab or window (using the normal systems for their web browser such as right clicking the link and picking "Open Link in New Tab) rather than just left clicking the link).

Is it at all possible to do this, or is it just generally best practice to reload the entire page in cases like this?


It's very much doable. You simply need to provide an href and an onclick in your links.

The href will activate if the user has no JS, or if the user decides to open the link in a special way (new tab, etc.)

The onclick will activate on "normal" clicks of the link. You can then cancel the default action (by returning false or using your JS lib of choice's way to do it) and do your ajax stuff.


It is possible, in fact its even possible to set up a timer to update portions of pages periodically. If you are using jquery it'd be something like this:

setInterval(function() {
         $('#your-div').load('your-server-side-request.php');
         }, 3000);

of course you could simply bind to a link, and on refresh use .load().

OR you could even just do this with normal javascript and use my script above as pseudocode essentially.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜