homepage Welcome to WebmasterWorld Guest from 54.198.139.141
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe and Support WebmasterWorld
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Calling a .js that is 12.9mb
javaScript, external .js, large file
Fess




msg:4531249
 12:19 am on Dec 27, 2012 (gmt 0)

Hello Webmasters,

Been away for a long time, good to be back..
Seeking constructive input from javaScript Artists as my knowledge is limited in this area.

When I started using the below script, the external .js was about 2.1mb (2007).
To date the external .js is about 12.9mb with over 100,000 lines of links.

Problem:
Calling a 12.9mb .js does not work in Firefox or it does not completely load. It works in Safari.


--------------------------------------------------------------THE SCRIPT

This is the external .js:

function GetRandom(start,end)
{
var range = end - start + 1;
var result = start + Math.floor(Math.random()*range);
return result;
}

function getRandomLink(whichSet) {
contentlinks = new Array("http://forums.example.org/showthread.php?f=154&t=449125",
"http://forums.example.org/showthread.php?f=154&t=451100",
"http://forums.example.org/showthread.php?f=154&t=449581",
"http://forums.example.org/showthread.php?f=154&t=450662",
"http://forums.example.org/showthread.php?f=154&t=449114");

trafficlinks = new Array("http://www.example1.com",
"http://www.example2.com/zbrush/home/home.php",
"http://www.example3.com/",
"http://www.example4.com/",
"http://www.example5.com/",
"http://www.example6.com/");

if (whichSet==0) { window.open(contentlinks[GetRandom(0,contentlinks.length-1)]) }
else if (whichSet==1) { window.open(trafficlinks[GetRandom(0,trafficlinks.length-1)]) }

}

--------------------------------------------------------------

This goes in between the <head> </head> tags:

<script type="text/javascript" src="http://www.mydomain.com/machoRand.js"></script>

--------------------------------------------------------------

This goes in between the <body> </body> tags:

<A href="#" onclick="getRandomLink(1);return false;"><font face="Arial"><u>resources</u></font></A>
<A href="#" onclick="getRandomLink(0);return false;"><font face="Arial"><u>amimations</u></font></A>

--------------------------------------------------------------

Can anyone give me some alternatives to calling the 1 big .js via putting this in in between the head tags <script type="text/javascript" src="http://www.mydomain.com/machoRand.js"></script> ?

Maybe I can have several smaller "machoRand.js"and have some code to pull random:
machoRand00.js
machoRand01.js
machoRand02.js
machoRand03.js


Any input is much appreciated
Best,
Fess

 

lucy24




msg:4531273
 1:29 am on Dec 27, 2012 (gmt 0)

TWELVE POINT NINE MEGABYTES ? ! ?

There. That's out of the way. Someone had to say it. Even my picture books don't go much beyond 2MB of image files-- and those are lots of separate pictures.

#1 and most obvious. javascript is perfectly happy being split into multiple files.* You can even put (global) variables into separate files; the functions will find them just fine. Can we please assume that it isn't one behemoth of an unsplittable function?

#2 gotta be asked too. Is the entire thing based on user response, so there's absolutely no alternative to javascript? That reference to "100,000 lines of links" would make a lot of people think of databases.


* I've got one package I use locally where all the global variable declarations are in one pair of files while the actual functions are in another, much smaller pair. Before anyone starts screaming, let me stress that "locally" aspect. It runs off my hard drive.

daveVk




msg:4531310
 4:28 am on Dec 27, 2012 (gmt 0)

Any chance of doing part or all of it the server, such inserting random here ?

<script type="text/javascript" src="http://www.mydomain.com/machoRand
N.js"></script>
Fess




msg:4531534
 7:48 pm on Dec 27, 2012 (gmt 0)

TWELVE POINT NINE MEGABYTES ? ! ?

There. That's out of the way. Someone had to say it. Even my picture books don't go much beyond 2MB of image files-- and those are lots of separate pictures.

#1 and most obvious. javascript is perfectly happy being split into multiple files.* You can even put (global) variables into separate files; the functions will find them just fine. Can we please assume that it isn't one behemoth of an unsplittable function?

#2 gotta be asked too. Is the entire thing based on user response, so there's absolutely no alternative to javascript? That reference to "100,000 lines of links" would make a lot of people think of databases.


* I've got one package I use locally where all the global variable declarations are in one pair of files while the actual functions are in another, much smaller pair. Before anyone starts screaming, let me stress that "locally" aspect. It runs off my hard drive.



Lucy, thanks for responding. I don't believe it is 'unsplittable'.
The entire code is user based, when user clicks on either of the following, a new random link spawns:

<A href="#" onclick="getRandomLink(1);return false;"><font face="Arial"><u>resources</u></font></A>

<A href="#" onclick="getRandomLink(0);return false;"><font face="Arial"><u>amimations</u></font></A>


Your database suggestion is one I've used for another purpose, the software is called phpAdsNew. But I've been away from all of this for a very long time.

Fess




msg:4531535
 7:57 pm on Dec 27, 2012 (gmt 0)

Any chance of doing part or all of it the server, such inserting random here ?

<script type="text/javascript" src="http://www.mydomain.com/machoRandN.js"></script>


Dave, thnks for responding.

That is exactly what I'm trying to find out.

If I can configure something like (PLEASE FORGIVE ME, NOT A JAVASCRIPT PRO)
machoRandSTRING.js = rand(machoRand00.js, machoRand01.js, machoRand02.js, machoRand03.js)

<script type="text/javascript" src="http://www.mydomain.com/machoRandSTRING.js"></script>

londrum




msg:4531536
 8:10 pm on Dec 27, 2012 (gmt 0)

[edit: sorry, didnt understand the question the first time. deleted the answer]

LifeinAsia




msg:4531540
 8:33 pm on Dec 27, 2012 (gmt 0)

It's not an issue of it not working in FF, it's an issue of almost no one is going to sit and wait for a 12M JS file to download on a site. And even if some people were, that's a huge waste of bandwidth.

That reference to "100,000 lines of links" would make a lot of people think of databases.

Gotta agree with this- this issue is just screaming for an AJAX implementation to a database.

rainborick




msg:4531736
 5:28 pm on Dec 28, 2012 (gmt 0)

A JavaScript file that large is just going to be a continuous source of problems. First of all, it's going to be terrible for your site's performance, and a horrible bandwidth hog - especially if it's used on many pages on your site. It would be much better to use a database to hold the URLs and use a PHP script to fetch the desired URL and redirect the user to it.

At least half of the job is already done for you by the forum software's database. Just add a separate table in the forum database to hold the 'trafficlinks'. You can use PHP to generate the JavaScript required to determine the number of forum URLs available for 'contentlinks' and 'trafficlinks'. Then it's just a matter of changing the window.open() calls in getRandomLink() to point to the PHP redirection script I mentioned above.

If this is beyond your experience at the moment, in the meantime you should be able to significantly reduce the size of your JavaScript file by removing the repetitious instances of 'http://forums.example.org/showthread.php?f=154&t=' in the contentlinks[] array, and just have it filled with the thread ID numbers. Then just use something like:

if (whichSet==0) { window.open('http://forums.example.org/showthread.php?f=154&t='+ contentlinks[GetRandom(0,contentlinks.length-1)]) }
else if (whichSet==1) { window.open(trafficlinks[GetRandom(0,trafficlinks.length-1)]) }
}

daveVk




msg:4531778
 10:58 pm on Dec 28, 2012 (gmt 0)

My suggestion was to do it on the server using say PHP, not in the javascript.

If you can not do it on the server then try

in head section replace

<script type="text/javascript" src="http://www.mydomain.com/machoRand.js"></script>

with

<script type="text/javascript">
function insertRandomScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'machoRand' + Math.floor(Math.random()*
10) + '.js'; // for 0 to 9
document.getElementsByTagName('head')[0].appendChild(script);
}
</script>

then on body tag

<body onload="insertRandomScript()">

Fess




msg:4531835
 5:27 am on Dec 29, 2012 (gmt 0)

My suggestion was to do it on the server using say PHP, not in the javascript.

If you can not do it on the server then try

in head section replace

<script type="text/javascript" src="http://www.mydomain.com/machoRand.js"></script>

with

<script type="text/javascript">
function insertRandomScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'machoRand' + Math.floor(Math.random()*10) + '.js'; // for 0 to 9
document.getElementsByTagName('head')[0].appendChild(script);
}
</script>

then on body tag

<body onload="insertRandomScript()">


LifeinAsia, rainborick, thanks for responding, I agree that the load time is impractical.

dave, thank you very much. That worked after cutting the script into 48 parts (300kb each) ;O) so its a random of 0-47.
Just a additonal question:

How would I call the script from another part of the server, i tried adding a full path but no luck:

script.src = 'home/someting/domain/machoRand' + Math.floor(Math.random()*48) + '.js';



<script type="text/javascript">
function insertRandomScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'home/someting/domain/machoRand' + Math.floor(Math.random()*48) + '.js'; // for 0 to 47
document.getElementsByTagName('head')[0].appendChild(script);
}
</script>



greatful..
~Fess

lucy24




msg:4531848
 6:16 am on Dec 29, 2012 (gmt 0)

i tried adding a full path but no luck:

script.src = 'home/someting/domain/machoRand

Wouldn't it need a leading slash just like any other absolute link? Starting from /machoRand meaning top-level directory.

:: irresistibly reminded of recent From Our Archives thread in thedailywtf dot com having to do with a not-very-random number ::

daveVk




msg:4531863
 11:48 am on Dec 29, 2012 (gmt 0)

Yes, slash on front or full url

'http://www.mydomain.com/home/someting/domain/machoRand' + ...

phranque




msg:4531865
 11:57 am on Dec 29, 2012 (gmt 0)

How would I call the script from another part of the server, i tried adding a full path but no luck

since the javascript resource is requested by the browser, the .js file must be web-accessible.
you can only get web access to subdirectories of the document root directory.

lucy24




msg:4531952
 1:00 am on Dec 30, 2012 (gmt 0)

'http://www.mydomain.com/home/someting/domain/machoRand'

My impression was that the "home/domain/etc." part referred to the physical path on the server, so you have to start from /machoRand. (Is that really the name of the function? :))

Backtracking a little bit: If I've understood it right, the code starts executing in response to user action. But at that point it pulls up something at random; the user isn't asking for one of those 100,000 URLs by name. If so, it seems like the random numbers could perfectly well be generated ahead of time, when the page is built, and kept in some type of holding bin. The user doesn't know, and doesn't need to know, that the numbers were really generated several seconds before he clicked. Store some reasonable number of them-- ten or a hundred or whatever is appropriate to the page-- and refresh the page if you run out.

Fess




msg:4532080
 10:32 pm on Dec 30, 2012 (gmt 0)

Backtracking a little bit: If I've understood it right, the code starts executing in response to user action. But at that point it pulls up something at random; the user isn't asking for one of those 100,000 URLs by name. If so, it seems like the random numbers could perfectly well be generated ahead of time, when the page is built, and kept in some type of holding bin. The user doesn't know, and doesn't need to know, that the numbers were really generated several seconds before he clicked. Store some reasonable number of them-- ten or a hundred or whatever is appropriate to the page-- and refresh the page if you run out.


daveVk, phranque that worked just fine. Much thanks!

Lucy, I agree that this script could be refined, but i'm just glad it's working for now. I will eventually take my buds suggestion and use software called TGProtator to get those text links generated ;o)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved