如何将浏览器内容的内容替换为xhr.responseText
How can I replace the content of the browsers content with the xhr.responseText
我想使用javascript或jquery更改整个页面的内容,并显示ajax请求返回的404 html页面。这可能吗?如何?
使用DOMParser将documentElement的内容替换为xhr.responseText
var dom = (new DOMParser).parseFromString(xhr.responseText, 'text/html');
// any attribute setting on `document.documentElement` etc here, then
document.documentElement.innerHTML = dom.documentElement.innerHTML;
dom = null;
浏览器支持解析HTML与DOMParser是
Google Chrome 30 +
Firefox 12 +
Internet Explorer 10 +
Opera 17 +
Safari 7.1 +
如果你需要支持传统的浏览器考虑而不是解析,从结尾搜索</html>
和字符串的开始为<html
,找到接近的>
,然后提取这两者之间的一切,然后被设置为innerHTML
可以使用最简单的$.ajax
快捷方法load()
if( someCondition ){
$('body').load('404.html');
}
这可能是你想要的,也可能不是,因为在问题
相关文章:
- xmlhttp.responseText不显示文本
- 中止POST XHR可靠
- 使用json_encode通过xmlhttp.responseText发送特殊字符(即caron)
- xmlhttp.responseText中显示的标记
- Dojo XHR将200 OK视为错误
- Chrome扩展:XHR后关闭窗口
- XHR不适用于聊天应用程序
- 是否可以缓存可下载的文件,并在XHR和非XHR请求之间共享该缓存
- 通过XHR请求将MYSQL数组从PHP返回到Javascript
- 将GET参数添加到q.io.xhr
- 为什么我得到responseText(通过Ajax从Servlet到javaScript)和额外的字符
- Winjs.xhr、Visual Studio和脱机服务器
- 访问通过XHR.onreadystatechange回调中的XHR.send(data)发送的数据
- xmlHttp.responseText未知错误定义
- 如何检索AMD的XHR响应代码(+时间戳)'大型Dojo
- Do jQuery stuff on xhr responseText
- 对xhr.responseText中的纯文本使用getElementsByName
- xhr.responseText returned null with POST
- 如何获取“数据”字段从xhr.responseText
- 如何将浏览器内容的内容替换为xhr.responseText