开发者

Load the main page if user visits a page meant for an ajax request?

I am using jQuery for a simple website and have a main page 'index.html' which can load some content (e.g. 'info1.html' or 'info2.html') via jQuery ajax requests, and display the result of these requests inside an element in the 'index.html' page.

If a user somehow visits say 'info1.html' directly, is their a way to redirect or load the main 'index.html' page? (or what's best practice for this type of thing) as Google is indexing all the small html files used for the ajax开发者_Python百科 requests and sometimes a user can click into the site via these pages.

Thanks.


Workaround 1:
since jquery will pick the content inside the body tag by default, you could add in the head element a script to redirect you to the main page or a meta tag ( more correct ) as Daniel mentioned.

Workaround 2:
if your ajaxed files are inside a folder you could use a robots.txt file and direct google to not index pages in those folders..

or you could do it directly from inside those pages (look at http://en.wikipedia.org/wiki/Noindex )


You may want to use a meta refresh tag:

<html>
<head>
    <meta http-equiv="refresh" content="0; url=http://mydomain.com/main.html">
</head>
<body>
    <div>
        <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed 
        ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad 
        eleifend option congue nihil imperdiet doming id quod mazim.</p>
    </div>
</body>
</html>


You can send a parameter when requesting the info1.html and info2.html. For example:

$.ajax({
                type: "POST",
                url: "info1.html",
                data:myParameter=1,
                success: function(html){
                    .........
                }
            });

Then in info1.html check if myParameter exists and if it is equal to 1. If both conditions are true you can display the page. If the conditions are not met you can redirect the user using the meta tag or with javascript. So if the user types info1.html directly in the url he will be redirected. For this you will need a server side technology such as PHP. So you would use info1.php and info2.php


I like Gaby's workarounds and I like to add a third one:

Replace your info to come from XML not HTML.

but if you want to keep the html you do not necessarily need a server side tech to redirect on page visit, You can use the window.onload event in java script to do this:

window.onload = function() {window.location.href = 'http://www.google.com';}  
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜