开发者

Ajax & Link Degradation

I load in HTML pages via Ajax. These files have no <title><head><body> tags, etc. Just a few divs inside them.

At the moment, I place links to these ajax pages in my href for browsers with JS disabled:

<a href="assets/ajax/honda.html">Honda</a>

The javascript has return false so the user is never taken to that page physically if their browser sup开发者_JS百科ports javascript.

My concern is people potentially right-clicking and sending these links to other people. Which would look bad since it is unstyled, etc. I'm tempted to remove the href link because of this.

Are there alternatives to obfuscating the links? It goes against my ideals on best practices to remove the link entirely from the href.


It goes against my ideals on best practices to remove the link entirely from the href.

I strive to follow best practices as well, but what you're doing is actually worse than not including an href at all.

The href attribute should only be used for URLs that users can visit directly. Using it to hold a URL for Ajax use is common (Stack Overflow does it), but it's a misuse of the href attribute.

If possible, href should point to a full page which contains the content that would be loaded by Ajax. If you can't provide that then either remove href or set it to something like "#".


You don't need to obfuscate it and I also don't think you need to remove it. If you are using a server side language you can check for the

X-Requested-With: XMLHttpRequest

HTTP Header in each page. If it isn't set it has not been requested with Ajax. Then you can redirect the user to the right page.


One solution that would work well, but includes some work, would be to create degradation pages for the content. You could create copies of the pages that were complete HTML documents, and use their URL in the links, e.g.:

<a href="assets/view/honda.html">Honda</a>

When you fetch the content using AJAX, you would replace the view in the URL with ajax.

This way the pages will also be indexed by web crawlers.


Maybe you could use the data tag to store/retrieve your value in a div with a mocklink style. FIDDLE

Something like this.

<div class="mockLink" data-link="/test/link/honda.html">Honda</div>

CSS

div.mockLink{color:blue; text-decoration:underline}
div.mockLink:hover{cursor:pointer;}

JS

$('div.mockLink').click(function(){
   alert($(this).data('link')) ;
   //do AJAX
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜