No-cache script without using JQuery

Hello I'm trying to create a script that inserts a meta tag into any web page to force no-cache .

This is currently my code and I don't want to use JQuery (as shown in Script to force IE8 cache behavior ).

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;
}

      

+1


source to share


1 answer


I would use ... prepend without JQuery:

parent.insertBefore(child, parent.firstChild);

      

parentNode.insertBefore (newChild, refChild);

Inserts a node newChild

as a child parentNode

before the existing child node refChild

. (Returns newChild

.)

If refChild

null, the children are added to the end of the list newChild

. Use equivalently and more readily parentNode.appendChild(newChild)

.

In this case, you will not need to scroll like in the code you provided.



UPDATE

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);

      

  • First create the meta tag as node.
  • Then take the title as a variable.
  • Using a variable, insert the node before the first child.

COMMENTS

  • Tested in Chrome.
  • Tested functionality in IE8 with console warning: "The code on this page disabled back and forth caching."
+1


source







All Articles