Eli*_*ria 20 html javascript ajax meta-tags
我有一个网站,我试图使用Ajax更新页面上的一些东西而不重新加载它.但是,我的很多用户很可能会使用不支持Javascript的移动浏览器,因此我尝试使用元刷新标签设计页面,这种方式只适用于没有Javascript的用户.有没有办法做到这一点?
我尝试将标签放在一个noscript元素中,但我的原始手机浏览器不会承认它.我想可能设置一个cookie来记住用户的浏览器是否支持Javascript,或者有一个版本的页面无需Javascript,并尝试使用Javascript将用户重定向到更复杂的版本,但我想知道是否有这是一种更优雅的方式.有没有人有任何想法?
Hri*_*shi 10
您无法使用JavaScript覆盖元刷新标记.
但是你可以这样做
假设您的页面位于 - >
http://example.net/mike.html 将以下代码放在那里 - >
<script type="text/javascript">
window.location = 'http://example.net/mike/for_Those_With_JavaScript_Enabled.html';
</script>
Run Code Online (Sandbox Code Playgroud)
scs*_*csc 10
我发现noscript标签对此非常有效.例如,您可以在关闭 head元素后放置它:
<noscript>
<meta http-equiv="refresh" content="5;URL=http://www.example.com">
</noscript>
Run Code Online (Sandbox Code Playgroud)
无需使用脚本删除元标记,因为具有脚本支持的浏览器将忽略noscript元素内的所有内容.
不幸的是,从@ bluesmoon的回答来看,操纵DOM不再适用了.
解决方法是检索原始标记,查找并替换元刷新元素,然后使用替换的标记编写新文档.
我不知道如何使用JavaScript检索原始标记,除非使用发送附加请求XMLHttpRequest.
在Opera中,我正在使用的是:
Disable meta refresh 1.00.js:
// ==UserScript==
// @name Disable meta refresh
// @version 1.00
// @description Disables meta refresh.
// @namespace https://stackoverflow.com/questions/3252743/using-javascript-to-override-or-disable-meta-refresh-tag/13656851#13656851
// @copyright 2012
// @author XP1
// @homepage https://github.com/XP1/
// @license Apache License, Version 2.0; http://www.apache.org/licenses/LICENSE-2.0
// @include http*://example.com/*
// @include http*://*.example.com/*
// ==/UserScript==
/*
* Copyright 2012 XP1
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*jslint browser: true, vars: true, maxerr: 50, indent: 4 */
(function (window, XMLHttpRequest) {
"use strict";
if (window.self !== window.top) {
return;
}
window.stop();
var uri = window.location.href;
var request = new XMLHttpRequest();
request.open("GET", uri, false);
request.send(null);
if (!(request.readyState === 4 && request.status === 200)) {
return;
}
var markup = request.responseText;
markup = markup.replace(/<meta http-equiv="refresh".*\/?>/gi, "");
var document = window.document;
document.open();
document.write(markup);
document.close();
}(this, this.XMLHttpRequest));
Run Code Online (Sandbox Code Playgroud)
Opera还具有内置功能,可禁用元刷新.不需要JavaScript.
在这种情况下,元标签很糟糕。那搜索引擎呢??
您应该做的就是使其像我在这里概述的那样。您的链接应指向完整的工作站点,就像 Web 2.0 页面一样。然后,通过事件处理程序 (onclick),您可以使用 ajax 增强用户体验。
因此,ajax 用户不会访问链接,而是在单击链接时进行处理,并将 ajax 请求发送到完全相同的 url,但使用 ajax GET 参数。
现在在服务器端,您必须能够通过某种方法生成整个站点。如果是ajax请求则发送相关内容。如果不是 ajax 请求,则生成嵌入了相关部分的完整站点。
您的网站将是SEO 友好的,可供移动用户使用,并针对现代浏览器和平台上的人们逐步增强。最后,ajax 生成的哈希链接将可用,甚至作为链接。
厉害了。:)
| 归档时间: |
|
| 查看次数: |
29885 次 |
| 最近记录: |