Solved

Preload page in iframe from dynamic array

Posted on 2008-10-02
3
233 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
  • 2
3 Comments
 
LVL 44

Expert Comment

by:scrathcyboy
Comment Utility
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
Comment Utility
@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
Comment Utility
>>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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now