Forum Moderators: open
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>
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).
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
<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.
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.
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>
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!
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.
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>
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...]