没有使用Jquery的no-cache脚本 [英] no-cache script without using Jquery
问题描述
您好我正在尝试创建一个脚本,在任何网页中插入元标记以强制禁止缓存。
Hello I am trying to create a script which inserts into any webpage a meta tag to force no-cache.
目前这是我的代码,我不想使用Jquery (如强制IE8缓存行为的脚本。
Currently this is my code and I dont want to use Jquery (as shown in Script to force IE8 cache behaviour).
var MAXlen = document.getElementsByTagName('head')[0].childNodes.length;
//Get the length of childnodes of head.
while(MAXlen--)
{
document.getElementsByTagName('head')[0].childNodes[MAXlen+1] = document.getElementsByTagName('head')[0].childNodes[MAXlen];
//store every node one place after.
if(MAXlen == 0)
document.getElementsByTagName('head')[0].childNodes[0].innerHTML = '<META HTTP-EQUIV="Pragma" CONTENT="no-cache">';
//place this hmtlcode into the first element of head.
break;
}
推荐答案
我会用...... prepend without JQuery:
I would use ... prepend without JQuery:
parent.insertBefore(child, parent.firstChild);
parentNode.insertBefore(newChild,refChild);
parentNode.insertBefore(newChild, refChild);
在现有子节点 refChild之前,将节点
。 (返回 newChild
作为 parentNode
的子节点插入 newChild
。)
Inserts the node newChild
as a child of parentNode
before the existing child node refChild
. (Returns newChild
.)
如果 refChild
为null , newChild
添加在子项列表的末尾。等效地,更可读的是,使用 parentNode.appendChild(newChild)
。
If refChild
is null, newChild
is added at the end of the list of children. Equivalently, and more readably, use parentNode.appendChild(newChild)
.
在这种情况下,你不会需要像你提供的代码一样循环。
In this case, you wouldn't need to loop through as in the code you presented.
更新:
尝试使用您的代码......
Try this with your code ...
var meta = document.createElement('meta');
meta.httpEquiv = "Pragma";
meta.content = "no-cache";
var head = document.getElementsByTagName('head')[0]
head.insertBefore(meta, head.firstChild);
- 首先,将元标记构建为节点。
- 然后,将head标签捕获为变量。
- 使用该变量,在第一个子节点之前插入节点。
评论:
- 在Chrome中测试功能。
- 在IE8中通过控制台警告测试功能:此页面上的代码禁用了后退和前进缓存
这篇关于没有使用Jquery的no-cache脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!