/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Place Script on Every Page or Just One?

Hi Everbody! CowGirl here!

My website has approximately 90 pages like [url]http://14kt.eu/index_2_with_script_x2.html[/url], all of which have the 2 scripts that appear on this page.

Assuming it is possible, would it be advisable (instead of placing both of these scripts on all of these approximately 90 pages) to place one or both of these scripts on one single page and simply have all the other pages refer to that one page with the script?

I thought of the above as part of my efforts to reduce the file size of these approximately 90 pages (I also replaced the <td> and <tr> with a <ul>). Of course, I just added the Google Analytics script to another website of mine and noticed that the Google Analytics website stated that the addition of the Google Analytics script to each page of my other website would only slow down the load time of the first page, as the script would be cached for the subsequent pages. I do not fully understand how the Google Analytics script would be cached (given it appears on each page), but if it would in fact be cached, I wonder if it would make any difference whether I put both these scripts on all approximately 90 pages or just one page (and refer to it on the other pages).

If anybody can clear up my confusion, both my cows and I would be most grateful!!!!!

Thank you, CowGirl!

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Aug 26.2008 — Normally you have to configure your server to cache your scripts. If you have them cached, they will not be downloaded for another page than the first. You can also minify them, and gzip them. Following the [url=http://developer.yahoo.com/performance/]Yahoo! High Performance[/url] rules will help too.

I'm not actually sure what you mean by "refer to it on the other pages", but minification, server configuration and gzipping are the way to go.

Another thing that will reduce your file size is a conversion to a full CSS layout. Unless you know the exact content that's going on the page, you cannot do that (because you cannot choose the correct tag if you don't know what's going in it), so bear that in mind too.

EDIT: I think you mean whether the scripts are external of not, right? External for sure, and if possible, combine them into one file and follow all the previous advice I've given.
Copy linkTweet thisAlerts:
@CowGirlauthorAug 27.2008 — Hi Declan1991,

Thank you very much for responding. Yes, when I said "refer to it on the other pages" I meant something along the lines of an external .css file (as opposed to an placing each and every css rule on each and every page) - but for scripts. I did not know if this was advisable (or even possible).

Can I simply save the first script as script_1.js in the "javascript" folder and place the following where the first script is now (right before the </header> tag): <script language="javascript" src="javascript/script_1.js"></script>?

Can I simply save the second script as script _2.js in the "javascript" folder and place the following where I want the second script to appear: <script language="javascript" src="javascript/script_2.js"></script>?

Unfortunately, I do not think it is possible to combine the scripts into one file. As to a conversion to a full CSS layout, I realize that would reduce the file size even more - but I wanted to wait and see if the website will be successful first, as I would need to hire somebody to do that.

Thank you for your time and big hugs, CowGirl!
Copy linkTweet thisAlerts:
@CowGirlauthorAug 27.2008 — Hi Declan1991,

Upon further thought, I do not think my above proposed course of action with regard to the second script will work because it has additional code after the </script> tag. (All of the below code is part of the tell a friend code - it includes an open and close <script> tag, followed by an open and close <a> tag.) If you know how to turn this into an external script, can you please let me know? Or should I do as I proposed above and just leave the open and close <a> on each and every page?

Again, thank you for you time and help - CowGirl!

<script language='JavaScript'>

function taf_55877f_080718195401(){var rtp;rtp = window.open( 'http://www.tellafriendking.com/refer/jgjgkjgkjgjkgjkgjg6aac9351aa3&URL=hgkjjkgjgjg.hjgjgjgejgjg.org', '_blank', 'width=350, height=530, resizable=yes, status=yes, scrollbars=yes');if((null==rtp)||'undefined'==typeof(rtp) ){alert( 'The tell a friend window was prevented from opening - trying the fall-back version instead.nnYou may want to temporarily disable your popup blocker.'); return false;}else{rtp.focus(); return true;}}

</script>

<a href='http://www.tellafriendking.com/refer/?CODE=jgkjgkjgjkjgac9351aa3&URL=http%3A%2F%2Fwww.gjkgkgkjgkjjhg.org' onclick='var pu=!taf_55877f_080718195401();return pu;' target='_blank' onmouseover="window.status='Tell A Friend!';return true;" onmouseout="window.status=''; return true;" >

THIS IS ALSO SCRIPT. THIS IS ALSO SCRIPT. THIS IS ALSO SCRIPT. Send this page to a friend.</a>
Copy linkTweet thisAlerts:
@rnd_meAug 27.2008 — you can store html snippets in javascript strings by encoding, escaping or otherwise protecting the javascript-ness of the code block.

you could also use the dom to build html tags to append/insert in your page.

be sure to put any script files that aren't need to render the page at the bottom of the body. the browser hangs while it downloads .src script tags, one-at-a-time. having the html already loaded when this occurs means users won't have to wait for the script to load to see the page.

the external files is always the same between 90 pages, so the cached version can be used for all pages.
Copy linkTweet thisAlerts:
@Declan1991Aug 27.2008 — You should be able to combine them with no problems, if they both work in separate script tags, they will work in the same script tag. So[code=html]
<script type="text/javascript" src="path/to/file.js"></script>[/code]
to link to the file. The inline JavaScript in the has to be left in the tag (you would have to rewrite the script to remove it), or find another script that does the same thing.

Note as well, I use a type attribute on the script tag, not a language one.
Copy linkTweet thisAlerts:
@spiresgateAug 27.2008 — Perhaps you are not seeing the wood for the trees.

Put ALL your javascript code in a text file and calll it Cowgirl.js

Save the file in the same directory as your other pages

Put

<script language="JavaScript" SRC="Cowgirl.js" type="text/javascript"></script>

in the header of each page.

It's just like an external css file.

Or have I misunderstood the problem?
Copy linkTweet thisAlerts:
@CowGirlauthorAug 27.2008 — Hi rnd me, Declan1991 and spiresgate,

Thank you for taking the time to post a response.

I inserted the script providing for the alternating color rows at http://www.14kt.eu/index_2_with_script_x2_externalscript.html (with the script being at http://www.14kt.eu/scripts/cowgirl_1.js), but it does not seem to want to work. I tried a few variations, but to no avail. Not sure what the problem is.

As I understood Declan1991's post, when they said "inline JavaScript in the has to be left in the tag", I assumed that this meant that the tell-a-friend script could not be placed in an external javascript file and had to be left in its current position on each and every page, unless the tell-a-friend script was redone. I sent an email to the tell-a-friend script provider asking if they could redo the script so it can be used on an external javascript file (have not received a response yet).

Did I misunderstand something?

Big Hugs (and thank you)! CowGirl!
Copy linkTweet thisAlerts:
@DokAug 27.2008 — Change you code in your script file from
[CODE]// JavaScript Document
<script type='text/javascript'>
// <![CDATA[
function FixRows(){
var oddclass = 'storelist';
var evenclass = 'storelistd';
var tableID = 'listings';// fill in table ID
var rows = document.getElementById(tableID).getElementsByTagName('tr');
var len = rows.length-2;
var j, o, temp;
for(var i = 3; i < len; i++){
rows[i].getElementsByTagName('td')[0].className = (i%2)?oddclass:evenclass;
};
};
window.onload = FixRows;
// ]]>
</script>[/CODE]

to
[CODE]// JavaScript Document
function FixRows(){
var oddclass = 'storelist';
var evenclass = 'storelistd';
var tableID = 'listings';// fill in table ID
var rows = document.getElementById(tableID).getElementsByTagName('tr');
var len = rows.length-2;
var j, o, temp;
for(var i = 3; i < len; i++){
rows[i].getElementsByTagName('td')[0].className = (i%2)?oddclass:evenclass;
};
};
window.onload = FixRows;
[/CODE]

You can not have html tags in .js files and you don't need the CDATA
Copy linkTweet thisAlerts:
@Declan1991Aug 27.2008 — function FixRows(){ var oddclass = 'storelist'; var evenclass = 'storelistd'; var tableID = 'listings'; var rows = document.getElementById(tableID).getElementsByTagName('tr'); var len = rows.length-2; var j, o, temp; for(var i = 3; i &lt; len; i++){ rows[i].getElementsByTagName('td')[0].className = (i%2)?oddclass:evenclass; }; }; window.onload = FixRows;
function taf_55877f_080718195401(){var rtp;rtp = window.open( 'http://www.tellafriendking.com/refer/jgjgkjgkjgjkgjkgjg6aac9351aa3&amp;URL=hgkjjkgjgjg.hjgjgjgejgjg.org', '_blank', 'width=350, height=530, resizable=yes, status=yes, scrollbars=yes');if((null==rtp)||'undefined'==typeof(rtp) ){alert( 'The tell a friend window was prevented from opening - trying the fall-back version instead.nnYou may want to temporarily disable your popup blocker.'); return false;}else{rtp.focus(); return true;}}
should be your external file. What I meant by the "inline JavaScript" was<i>
</i>&lt;a href="anything" [b]onmouseover="javascript"[/b]&gt;
Copy linkTweet thisAlerts:
@CowGirlauthorAug 27.2008 — Hi Dok and Declan1991,

Thank you (in the case of Declan1991, again) for taking the time to post your replies. Not only does everything work fine now, but you taught me a little about scripts!

Thank you very much for your help. I have now managed to substantially reduce the file sizes of my pages - the code is lean and mean!

Totally big hugs, CowGirl!!!!!!!!!!!!!!!!!!!!!!
×

Success!

Help @CowGirl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...