Forum Moderators: open

Message Too Old, No Replies

Heavy Script

         

Fess

5:32 pm on Aug 14, 2004 (gmt 0)

10+ Year Member



Hello everyone,

This post is regarding my older thread:
[webmasterworld.com...]

My problem is......the script has grown very heavy.

The "contentlinks" and the "trafficlinks" parts of the function have a total of 1000+ urls to call.

So every time my page loads, I has to load all that in the back ground which stalls that page for a few seconds on cable connection.

Is it at all possible to have the urls in a separate .txt file and just have the script call the urls from the .txt file.

This should eliminate the huge load time it takes to load all the urls in the page?

Any advice is appreciated
Regards
Fess

Currently my script is this (pasted between the <head></head> tags):

<script language="JavaScript">

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://www.example.com",
"http://www.example.com",
"http://www.example.com",
"http://www.example.com",
"http://www.example.com",
"http://www.example.com");

trafficlinks = new Array("http://www.example.com",
"http://www.example.com",
"http://www.example.com",
"http://www.example.com",
"http://www.example.com",
"http://www.example.com");

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

}

</script>
</head>

Bernard Marx

7:08 am on Aug 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll have a go (or two)

1. Put the script into an external .js file. Then either

a) try using the defer attribute:

<script src="randomlink.js" defer="defer" type="text/javascript">

which will allow the page to be rendered while the file is downloading.
ref: defer attribute [w3schools.com]

b) or put the <script> tag at the bottom of the page, just before the closing body tag.

2. #1 still involves all that info being downloaded, whilst only one URL is actually used. This could be solved with PHP, ASP. Is ASP available? I could have a go at that (I'm guessing it would be quite easy).

Fess

5:22 pm on Aug 20, 2004 (gmt 0)

10+ Year Member



Hello Bernard

Thanks for the response

2. #1 still involves all that info being downloaded, whilst only one URL is actually used. This could be solved with PHP, ASP. Is ASP available? I could have a go at that (I'm guessing it would be quite easy).

My goal is to not have all the unused info downloaded.
ASP is not possible as I'm on a Unix server.
Php is possible....

Thanks
Fess

Bernard Marx

9:53 pm on Aug 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use an Apache server that runs ASP, so perhaps...
..but if you only have PHP, I still think it would be a piece of cake (I just don't know how it's done).

StupidScript

11:28 pm on Aug 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



(Sorry, Bernard :)
For HTML 4.0, the defer syntax should be:

<script src="randomlink.js" defer=1 type="text/javascript"></script>

Using PHP, you would do nearly the same thing as you are doing with Javascript:

<script type="text/javascript">
<?
$contentlinks = array(
"http://www.example.com",
"http://www.example.com"
);
$trafficlinks = array(
"http://www.example.com",
"http://www.example.com"
);
if ($whichset) {
print("window.open($trafficlinks[rand(0,sizeof($trafficlinks)])");
}
else {
print("window.open($contentlinks[rand(0,sizeof($contentlinks)])");
}
?>
</script>

The advantage of the server-side compilation of PHP or ASP (or JSP or whatever) is that the random URL selection and the writing of the result will take place at the server before the page is delivered to the client. The full array will never be downloaded.

Bernard Marx

12:01 am on Aug 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For HTML 4.0, the defer syntax should be:...

Well..some references say

[blue]"defer"[/blue]
, others say
[blue]"true"[/blue]
, MS goes for the old-style no valueless attribute, I can't work out what W3C is saying the 1¦2 valid values are.

My guess is that pretty much any value (or none at all) will do the trick in most browsers, with the probable exception of

[blue]0[/blue]
or
[blue]false[/blue]
.

Having said that, MS's version, and yours are definitely XML invalid (no attribute and unquoted, respectively). So there ;)

I think we'd worked out the "why" for the server-scripting, but that's a very nice implementation of the "how". I think I'll plug it in and try it myself too. Thanks.



I've just taken a look through this article [scott.yang.id.au] that I found earlier. The comments feature a minor flame war on the subject of the obviously highly inflammatory topic of The correct value for the defer attribute. Conclusion?

Fess

5:50 pm on Aug 21, 2004 (gmt 0)

10+ Year Member



I use an Apache server that runs ASP, so perhaps...
..but if you only have PHP, I still think it would be a piece of cake (I just don't know how it's done).

I'm asking my hosting guys for some direction with this issue as well. They know more about php than I have time to learn.

Regards
Fess

Bernard Marx

7:05 pm on Aug 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I haven't tried yet, but I reckon StupidScript has supplied the template. Just expand the arrays, put it into your .htm file, then change the extension to .php .

Bernard Marx

10:54 pm on Aug 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've had a little go with StupidScript's PHP script. I'm the original neophyte when it comes to PHP, so I didn't notice that each print statement is missing a closing bracket somewhere. I learnt some PHP in the debugging attempts before I noticed.

It still causes a parsing error though, so I messed around, and simplified until I got a form that works for me. The script simply creates adds these lines before the open() statement:

var contLink = "_one_of_the_cont_URLS_";
var trafLink = "_one_of_the_traf_URLS_";

Here 'tis: ---------------------------------------


<html><title>StupidScript's script (messed around with)</title>
<head>
<script type="text/javascript">


<?php

$contentlinks = array(
"http://www.yahoo.com",
"http://www.quirksmode.org",
"http://www.twinhelix.com"
);
$trafficlinks = array(
"http://www.webmasterworld.com",
"http://us2.php.net/manual/en/",
"http://www.jibbering.com/faq/"
);

print("var contLink = \"".$contentlinks[rand(0,sizeof($trafficlinks))]."\";\n");
print("var trafLink = \"".$trafficlinks[rand(0,sizeof($trafficlinks))]."\";\n");

?>

window.open(contLink,"winname","width=200,height=200");

</script>
</head>
<body>

</body>
</html>

StupidScript

4:31 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you, Bernard! Your "refer" comments caused me to return to the W3C spec and investigate more closely. Amusingly, the flame war you linked to, and the examples you and I cited, all seem to be incorrect.

I'm going to stand by the following interpretation of the spec...at least until further corrections are necessary. :)

Here is my take on implementing the "defer" attribute in a SCRIPT tag:

<script language="javascript" type="text/javascript" src="site.js" defer>
</script>

The presence of the attribute triggers the deferral. In the W3C spec, there is no value indicated, rather that the "defer" attribute is itself a boolean attribute, and including it as I have, above, sets its value to "true" or "1", while omitting the attribute sets its value to "false" or "0".

IMHO.

You found a glaring error in my PHP/Javascript code, for which I also thank you.

The Javascript "window.open" function does require delimiters around the URL parameter...plus I forgot to close the parentheses in the "rand" function.

Adding delimiters ("") in the PHP "print" function should satisfy (I also tidied up the end-of-statement by including a semi-colon and a new-line):

print("window.open(\"$contentlinks[rand(0,sizeof($contentlinks))]\");\n");

I appreciate your expanding the example for Fess, and others. Good job, and thanks again!

Bernard Marx

4:51 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The one rule I thought I knew was that all attributes must have values. Hence things like
multiple = "multiple"
. But then again, that is strict XHTML not HTML.

You might have noticed that I don't often post outside the Javascript forum. The reason being that what I know about anything else would fit in a matchbox and still leave room for all the matches.

About the delimiting/quoting. In my ver, this wasn't needed, because the URLs are held in the variable, contLink, which is passed into the function. In yours, the PHP writes directly into the function call.

I chose the variable approach just to make it general. (As you know) I changed it later!

Funnily enough, today, dcrombie posted a shortcut we could have used:
[webmasterworld.com...]

Thanks for the PHP diversion. Good fun.

StupidScript

5:14 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only other example that comes to mind (an attribute with no value) is in the FRAME tag of a frameset, where the "noresize" attribute has no value, being, itself, a boolean attribute.

i.e.
<frameset rows="*,*">
<frame src="top.html" noresize />
<frame src="bottom.html" />
</frameset>

How does XML view that? (I haven't been involved with writing schemas since SGML was the law of the land. I look forward to having enough time to dig deeply into XML...but haven't yet. :)

Oh...also
<select name="dropdown">
<option value="1" selected />One
<option value="2" />Two
</select>

Bernard Marx

5:43 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I had a couple of things to say about that

1. Frames don't exist in strict XHTML (need frames DTD).
(iframes excepted)
2. noresize="noresize"

..but I thought I'd check 'em first!

Found what appears to be quite a good overview:

XHTML frames in general: [academ.hvcc.edu...]
XHTML noresize attribute: [academ.hvcc.edu...]

StupidScript

5:59 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, Bernard. Those are very interesting references.

I guess I like the idea of every attribute needing a value. It sure cuts out the confusion!