Click to See Complete Forum and Search --> : external .js file
feichter
11-16-2003, 11:30 AM
Can anyone help with this?
I am trying to insert the code below in an external .js file, but it does not work. It works fine when inserted directly into the page.
<SCRIPT type="text/javascript">
var firstCall = true;
function changeFontsize(fSize, increment) {
if (firstCall) {
firstCall = false;
if (increment != "")
changeFontsize('18', '');
}
if (document.getElementsByTagName) {
tags = new Array ("p", "a");
for (j=0; j<tags.length; j++) {
var getElement = document.getElementsByTagName(tags[j]);
var eachElement, currentFontSize, fontIncrease, newFontSize;
for (i=0; i<getElement.length; i++) {
eachElement = getElement[i];
if (increment != "") {
currentFontSize = parseInt(eachElement.style.fontSize);
fontIncrease = parseInt(increment);
newFontSize = currentFontSize + fontIncrease;
}
else if (fSize != "")
newFontSize = parseInt(fSize);
eachElement.style.fontSize = newFontSize + "px";
}
}
}
}
</SCRIPT>
The link on the page is like this:
<a href="javascript:changeFontsize('', '2');" title="Increase the size of the font">+</a>
Many thanks.
fredmv
11-16-2003, 12:27 PM
Just like with external CSS, don't include any HTML in it (remove the opening and closing <script> tags).
feichter
11-16-2003, 01:38 PM
Sorry, forgot. I didn't include the <script> tags.
How are you calling the file?
feichter
11-16-2003, 02:10 PM
Like this:
<SCRIPT LANGUAGE="JAVASCRIPT" SRC="/scripts/various.js">
</SCRIPT>
James L.
11-16-2003, 02:34 PM
<script type="text/javascript" src="scripts/various.js"> </script>
feichter
11-17-2003, 02:22 PM
Still stuck with this one. I have other javascript running externally, with html, etc., but this one just refuses to work!
fredmv
11-17-2003, 02:29 PM
Do you possibly have a link to the page so we can see this in action? It may make things easier for all of us. :D
feichter
11-17-2003, 02:38 PM
The webpage is: http://www.holidayhomefrance.net/text.php
The script page is: http://www.holidayhomefrance.net/scripts/various1.js
Thanks.
dragle
11-18-2003, 08:44 AM
Hi freichter,
The code you posted above is not identical to the code posted in the external JS file on your site. Specifically, this line:
for (i=0; i<getElement.length; i++) {
looks like this in your external file:
for (i=0; i>getElement.length; i++) {
and therein lies your problem (note the reversed angle bracket, which is short circuiting your loop).
Cheers!
feichter
11-18-2003, 01:31 PM
Dan
Thanks for your reply, however this does not solve my problem. Why does the script work when inserted into the head of the page, but not when in an external file?
dragle
11-18-2003, 01:52 PM
Because the script you applied to the head of the page is the same as the script you posted in this thread, in which the angle bracket is facing the right direction, perhaps? Don't know for certain, but if you post the two pages, i.e, the one you've already posted and an additional one with the script literally in the head of the page that works, then perhaps we can compare the two and make further headway.
Cheers!
feichter
11-18-2003, 04:34 PM
Have a look at http://www.holidayhomefrance.net/text1.php with the javascript in the head tags.
This works fine on my system. (Click on the + sign)
Thanks
dragle
11-18-2003, 04:56 PM
Yup, and just like the original problem, the script you have posted in the page is different from the script as it stands in your external file. In text1.php you have this:
for (i=0; i<getElement.length; i++) {
But in your external file (http://www.holidayhomefrance.net/scripts/various1.js ), you have this:
for (i=0; i>getElement.length; i++) {
You're calling two different functions, therefore you should expect to get two different results.
Or perhaps looking at it this way will help. The problem is not with the external file call, the external file is being loaded fine. The problem is that the function that you are calling on the two pages is different between the external version and the in-page version.
HTH,
feichter
11-22-2003, 09:09 AM
Thanks for pointing this out Dan and I have changed the files accordigly but it still does not work!!!!!!
Anymore help? Or is everone as fed up as me !!
dragle
11-24-2003, 09:05 AM
Hiya,
When I look at your files from this side, I do not see the changes. I.E., there's still that key difference in the scripts between the external file (http://www.holidayhomefrance.net/scripts/various1.js) and the text1.php file (http://www.holidayhomefrance.net/text1.php).
Cheers!