[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

Preload page in iframe from dynamic array

I unsuccessfully combined two solutions by hielo into one script:

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23766062.html
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23755730.html

The problem is that the entire array is placed into the first cookie, and remaining cookies are undefined.

I think the cause is the format of the dynamically created array.

Can you please fix this?
Thanks!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title>Preloader Demo</title>
</head>
<body>
 
<h1>Hello World</h1>
 
<p>This is a test...</p>
 
<a href="http://www.google.com/">Google</a>
<a href="http://en.wikipedia.org/">Wiki</a>
<a href="http://www.msn.com/">MSN</a>
<a href="http://www.blogger.com/">Blogger</a>
 
 
<div id="speeder">
<script type="text/javascript">
/* <![CDATA[ */
var linkList=new Array();
var linkType=2;  // 0 for none, 1 for local only, 2 for remote only, 3 for both
 
var loadPages=new Array(report());
var counter=0;
var PLinitIAL_DELAY=500; //milliseconds
var PAUSE_DURATION=20; //milliseconds
var PAGES_PER_CALL=3;
var container = document.getElementById('speeder');
container.style.width="1px";
container.style.height="1px";
container.style.overflow="hidden"
container.style.display="block";
container.style.position="absolute";
container.style.left="-200em"; 
 
function PLcreateCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
 
function PLreadCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
 
function PLeraseCookie(name) {
	PLcreateCookie(name,"",-1);
}
 
function PLsetIt(n,v){
	PLcreateCookie(n,v,30);
	++counter;
	if( loadPages.length )
	{
		PLmakeframes();
	}
	return true;
}
 
function PLmakeframes(){
 
	var limit = loadPages.length > PAGES_PER_CALL ? PAGES_PER_CALL : loadPages.length;
	var cacheURL='cacheURL'+ counter;
 
	if(!PLreadCookie(cacheURL))
	{
		var frm  = document.createElement("iframe");
		frm.src = loadPages.shift();
		if( window.ActiveXObject )
			frm.onreadystatechange=function(){if(frm.readyState=="complete")PLsetIt(cacheURL,frm.src);
 	}
		else
			frm.onload=function(){PLsetIt(cacheURL,frm.src); 
	}
		  
		container.appendChild(frm);
	}
        else if(loadPages.length)
        {
		loadPages.shift();
		++counter;
		PLmakeframes();
        }
 
	return true;
}
 
function report()
{
	
        linkList.length=0;
        switch(linkType){
        case 0:
                break;
        case 1:
                        var re = new RegExp("^http?:\\/\\/" + location.hostname,"i");
                for( var i=0, limit=document.links.length; i < limit; ++i)
                {
                        if( re.test(document.links[i].href) )
                        {
                                linkList.push(document.links[i].href);
                        }
                }
                break;
 
        case 2:
                        var re = new RegExp("^http?:\\/\\/" + location.hostname,"i");
                for( var i=0, limit=document.links.length; i < limit; ++i)
                {
                        if( !re.test(document.links[i].href) )
                        {
                                linkList.push(document.links[i].href);
                        }
                }
                break;
        case 3:
	   		for( var i=0, limit=document.links.length; i < limit; ++i){
               	linkList.push(document.links[i].href);
			}
                break;
        }
	return linkList;
} 
 
function PLinit(){
	if(window.top == window) setTimeout("PLmakeframes()",PLinitIAL_DELAY);
}
 
window.onload=PLinit;
/*	]]> */
</script>
</div>
 
</body>
</html>

Open in new window

0
hankknight
Asked:
hankknight
  • 2
1 Solution
 
scrathcyboyCommented:
you will have to ask hielo to fix his code, others can't do it for him.  Click the red button, right of your question, request attention and ask the MOD to get hielo.
0
 
hieloCommented:
@scrathcyboy:"Click the red button, right of your question, request attention and ask the MOD to get hielo."
Please do not make this suggestion again. Unfortunately some people here have the "power" to post "garbage posts" on other people's profile which one cannot delete and just makes the page load ever so slowly due to the amount of "garbage posts" already there. Personally, seeing messages asking me to help others on that profile page is a waste. I already help a lot of people without the need of anyone to leave me garbage posts there. I find that practice quite infuriating.
0
 
hieloCommented:
>>The problem is that the entire array is placed into the first cookie
report() returns an array so you do not need:

var loadPages=new Array(report());

what you need is:
var  loadPages=report();
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now