问题:
HTML 页面中有一组常规的 URL 链接,例如:
<a href="/foo/bar">Foo Bar</a>
您想要创建一个 JavaScript 函数,以便在单击任何 HTML 链接时,客户端的浏览器不会导航到该新 URL“ /foo/bar”改为执行 JavaScript 函数(例如。例如,这可以进行 Ajaxian 调用并加载 HTML 数据,而无需重新加载页面)。
但是,如果 JavaScript 被禁用或蜘蛛爬行该网站,则 UTL 链接将得到妥善维护。
这可能吗?它已经存在了吗?通常的做法是什么?
编辑1:
这些是一些很好的答案!
只是一个后续问题:
如果用户单击后退按钮或前进按钮,这自然会中断(因为它会返回到其所在的最后一个物理页面,而不是加载的页面)。
有什么方法(跨浏览器)来维护后退/前进按钮?
(例如,创建单击的链接数组并覆盖浏览器按钮并使用该数组进行导航)?
Problem:
You have a regular set of URL links in a HTML page e.g.:
<a href="/foo/bar">Foo Bar</a>
You want to create a JavaScript function such that when any HTML links are clicked, instead of the client's browser navigating to that new URL "/foo/bar" a JavaScript function is executed instead (e.g. this may for example make an Ajaxian call and load the HTML data without the need to reload the page).
However if the JavaScript is disabled OR a spider crawls the site, the UTL links are maintained gracefully.
Is this possible? Does it already exist? What's the usual approach?
EDIT 1:
These are some great answers!
Just a follow on question:
If the user clicks on the back button OR forward button, this would naturally break (as in it would go back to the last physical page it was on as opposed to one that was loaded).
Is there any way (cross browser) to maintain the back/forward buttons?
(e.g create an array of links clicked and over ride the browser buttons and use the array to navigate)?
发布评论
评论(6)
如果 Javascript 关闭,链接将正常运行。
在这种情况下,除非
your_function()
不返回 false,否则单击链接时也会跟随该链接。为了防止这种情况,可以让
your_function()
返回 false,或者在 onclick 属性中的函数调用之后添加return false;
:或者:
使用 element.addEventListener()
点击后默认锚点行为:
没有:
If Javascript is off, the link behaves normally.
In this case, unless
your_function()
does not return false, the link will be followed when clicked as well.To prevent this, either make
your_function()
return false, or addreturn false;
just after the function call in your onclick attribute:Or:
Using element.addEventListener()
With default anchor behaviour following click:
Without:
鉴于当前的 HTML 和 W3C API,我会选择:
在标记中,
linkify.js
包含类似以下内容:请参阅 http://jsfiddle.net/alnitak/nrC7G/,或 http ://jsfiddle.net/alnitak/6necb/ 适用于不使用
window.onload
的版本。请注意,此代码使用在
document
对象上注册的单个侦听器函数,该函数将作用于每个标记本身不会捕获点击。
Given current HTML and W3C APIs, I would go for:
in the markup, with
linkify.js
containing something like:See e.g. http://jsfiddle.net/alnitak/nrC7G/, or http://jsfiddle.net/alnitak/6necb/ for a version which doesn't use
window.onload
.Note that this code uses a single listener function registered on the
document
object, which will act on every<A>
tag on the page that doesn't trap clicks for itself.使用
onclick
属性:return false
会阻止默认行为,但在没有 JavaScript 的情况下,链接将被跟踪。Use an
onclick
attribute:The
return false
prevents the default behaviour, in the absence of JavaScript, however, the link will be followed.http://jsfiddle.net/bTuN7/
全部在脚本内完成,如果 JavaScript 不会“伤害”不起作用。
如果您考虑 AJAX,那么您必须知道,googlebot 会尝试解析它。 http://www.youtube.com/watch?v=9qGGBYd51Ts
http://jsfiddle.net/bTuN7/
All done inside script and it won't 'hurt' if JavaScript doesn't work.
If you think about AJAX, then you have to know, that googlebot tries to parse it. http://www.youtube.com/watch?v=9qGGBYd51Ts
您可以这样编写代码:
如果 JavaScript 被禁用或者是某些网络爬虫,则不会执行 JavaScript,因此从我的角度来看,这是更好的选择。
You can code like:
JavaScript is not executed in case it's disabled or if it's some web crawler, so from my point of view this is preferable.
有很多方法,例如:
http://www.malbecmedia.com malbecmedia.com/blog/development/coding-a-ajax-site-that-degrades-graceously-with-jquery/
但请记住,凭借良好设置的服务器和缓存,您将不会通过 Ajax Load 获得更高的性能。
There's quite a few methods out there such as this:
http://www.malbecmedia.com/blog/development/coding-a-ajax-site-that-degrades-gracefully-with-jquery/
Remember, though, that by virtue of a well setup server and caching you're not going to gain yourself much performance with an Ajax Load.