Solved

Preload page in iframe from dynamic array

Posted on 2008-10-02
3
280 Views
Last Modified: 2012-05-05
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
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 22629388
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
 
LVL 82

Expert Comment

by:hielo
ID: 22637000
@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
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 22638961
>>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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question