473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Include js files

Hi, I am making a js file that increases font sizes by its relative
size. It works fine as all one file with the script in the html file
but when i go to put the js in a seperate file and reference it as a
source file, it doesn't work properly. What happens is that when i go
to increase the font size, it goes lower(the first time), but the next
time it increase and increases, and decreases, which is fine. But
whatever happens, it sets it to a different start value, which is
wrong.

here's the code for the html file:
-------------------------------------------------------------------
<html>
<head>
<SCRIPT TYPE="text/javascript" SRC="textsize2.js"></SCRIPT>
</head>
<body>

<h1>Hello there</h1>
<h2>Test</h2>
anything.

<input type="button" onclick="resizeBodyText(2, 'n')" value="Font +" >
<input type="button" onclick="resizeBodyText(-2, 'n')" value="Font -" >

<input type="reset" onclick= "resizeBodyText(0, 'y')">

</body>
</html>
------------------------------------------------------------------

And here's the code for the js file:

-------------------------------------------------------------------
var current = parseInt(getCookie("fontFactor"))
if (isNaN(current))
current= 0;

resizeBodyText(current, "n")
function resizeBodyText(factor, reset)
{
if (reset=="y")
factor= (current * -1);

window.alert(current + " " + factor)
//------------------------------------------------
var a = document.all;
var s = '';
current += factor;

if (current < 0)
current = 0;
else
for (var i = a.length-1; i >0;i--)
{
s=a[i].currentStyle.fontSize+'';
s=Right(s,2);
a[i].style.fontSize = parseInt(a[i].currentStyle.fontSize)+factor+s;
}
setCookie("fontFactor", current)
}

//-----------------------------------------------
function Right(str, n) {
if (n <= 0)
return "";
else if (n > String(str).length)
return str;
else {
var iLen = String(str).length;
return String(str).substring(iLen, iLen - n);
}
}
//-------------------------------------------------
function getCookie(name) {
var dc = document.cookie;
var index = dc.indexOf(name + "=");
if (index == -1) return null;
index = dc.indexOf("=", index) + 1; // first character
var endstr = dc.indexOf(";", index);
if (endstr == -1) endstr = dc.length; // last character
return unescape(dc.substring(index, endstr));
}
function setCookie(name, value)
{
document.cookie= name + "=" + escape(value);
}
-------------------------------------------------------------------

If someone could help me, it would be greatly appreciated! Thanks

JF

Jul 23 '05 #1
2 3231
jfancy-Transport Canada wrote:
Hi, I am making a js file that increases font sizes by its relative
size. It works fine as all one file with the script in the html file
but when i go to put the js in a seperate file and reference it as a
source file, it doesn't work properly. What happens is that when i go
to increase the font size, it goes lower(the first time), but the next
time it increase and increases, and decreases, which is fine. But
whatever happens, it sets it to a different start value, which is
wrong.

It seems pointless to replicate a function that is provided by a
browser in a much more efficient manner than you can do, but let's have
fun anyway.

I ignored the cookie stuff.
here's the code for the html file:
-------------------------------------------------------------------
<html>
<head>
<SCRIPT TYPE="text/javascript" SRC="textsize2.js"></SCRIPT>
</head>
<body>

<h1>Hello there</h1>
<h2>Test</h2>
anything.

<input type="button" onclick="resizeBodyText(2, 'n')" value="Font +" >
<input type="button" onclick="resizeBodyText(-2, 'n')" value="Font -" >

<input type="reset" onclick= "resizeBodyText(0, 'y')">

</body>
</html>
------------------------------------------------------------------

And here's the code for the js file:

-------------------------------------------------------------------
var current = parseInt(getCookie("fontFactor"))
You should always use parseInt with a radix. In this case, probably
better to use parseFloat anyway (font size units may be decimal). I
just set current to 0 and removed the cookie stuff.
if (isNaN(current))
current= 0;

resizeBodyText(current, "n")
This will attempt to run your resizeBodyText function probably before
the document has finished loading. Run it from an onload (either in
the HTML source or using window.onload).
function resizeBodyText(factor, reset)
{
if (reset=="y")
factor= (current * -1);

window.alert(current + " " + factor)
//------------------------------------------------
var a = document.all;
This will only work in browsers that support document.all, so feature
test first:

if (document.all) {
var a = document.all;
} else {
return; // No point in continuing
}

or add an alternative if you add cross-browser support for
currentStyle:

if (document.getElementsByTagName) {
var a = document.getElementsByTagName('*');
} else if (document.all) {
var a = document.all;
} else {
return; // No point in continuing
}
var s = '';
current += factor;

if (current < 0)
current = 0;
else
?
Don't understand the logic of the if..else. Why not just the
following 'for'?
for (var i = a.length-1; i >0;i--)
{
s=a[i].currentStyle.fontSize+'';
s=Right(s,2);
a[i].style.fontSize = parseInt(a[i].currentStyle.fontSize)+factor+s;
}
This seems cumbersome. Try this, it eliminates Right() and tests for
currentStyle & style support before trying to use them:

// exit if currentStyle or style not supported
if (!a[0].currentStyle || !a[0].style ) return;

var s, an, au, i=a.length;
while ( --i ) {
s=a[i].currentStyle.fontSize;
an = parseFloat(s); // Get the number part
au = s.replace(an,''); // Get the units
a[i].style.fontSize = an + factor + au;
}

Note that use of currentStyle is likely IE only (certainly won't work
in Firefox).

If you want to add support for other browsers, do a search in the news
group for getComputedStyle - you will likely want to create your own
function that uses one or the other depending on browser support.

You may realise that the above avoids a[0], which is the HTML
element. Setting the font size there will give all elements the same
size font (this may not have been intended originally, but that's what
happens).
setCookie("fontFactor", current)
}

//-----------------------------------------------
function Right(str, n) {
if (n <= 0)
return "";
else if (n > String(str).length)
return str;
else {
var iLen = String(str).length;
return String(str).substring(iLen, iLen - n);
}
}
No longer required

[...]
If someone could help me, it would be greatly appreciated! Thanks


The buttons should only appear in browsers that support the features of
your script, so maybe test that right up front and only show them if
they're going to work.

The resize function is now:

function resizeBodyText(factor, reset) {
if (reset=="y"){
factor = (current * -1);
}

current += factor;

if (document.getElementsByTagName) {
var a = document.getElementsByTagName('*');
} else if (document.all) {
var a = document.all;
} else {
return; // No point in continuing
}

// exit if currentStyle or style not supported
if (!a[0].currentStyle || !a[0].style ) return;

var s, as, au, i = a.length;
while ( --i ) {
s = a[i].currentStyle.fontSize;
an = parseFloat(s); // Get the number part
au = s.replace(an,''); // Get the units
a[i].style.fontSize = an + factor + au;
}

setCookie("fontFactor", current)

}


--
Rob
Jul 23 '05 #2
RobG wrote:
jfancy-Transport Canada wrote:
Hi, I am making a js file that increases font sizes by its relative
size. It works fine as all one file with the script in the html file
but when i go to put the js in a seperate file and reference it as a
source file, it doesn't work properly. What happens is that when i go
to increase the font size, it goes lower(the first time), but the next
time it increase and increases, and decreases, which is fine. But
whatever happens, it sets it to a different start value, which is
wrong.

It seems pointless to replicate a function that is provided by a
browser in a much more efficient manner than you can do, but let's have
fun anyway.

[...]
Note that use of currentStyle is likely IE only (certainly won't work
in Firefox).

If you want to add support for other browsers, do a search in the news
group for getComputedStyle - you will likely want to create your own
function that uses one or the other depending on browser support.


Below is a cross-browser version, tested in IE 6 and Firefox. One
issue is that it loads, then does the re-size so in larger pages there
will be some issues when the page re-sizes. Highlights that this is
not a good idea.

<!-- new body tag in HTML -->

<body onload="resizeBodyText('onload','n')">

/* Revised script */

// Setup cross browser functions
// Get all elements
var getAll;
var getFontSize;

function initFunctions(){
if (document.getElementsByTagName) {
getAll = function() {return document.getElementsByTagName('*')};
} else if (document.all){
getAll = function() {return document.all};
} else {
getAll = function() {return false};
}

// Get element font size
var x = document.body;
if ( x.currentStyle ) {
getFontSize = function(el) {
return el.currentStyle.fontSize;
}
} else if ( document.defaultView.getComputedStyle(x,'').fontSi ze ){
getFontSize = function(el) {
return document.defaultView.getComputedStyle(el,'').fontS ize;
}
} else {
getFontSize = function(el) {
return false;
}
}
}

// Resize text
function resizeBodyText(factor, reset) {
if (reset=="y"){
factor -= current;
}

if ( 'onload' == factor ) {
factor = current;
} else {
current += +factor;
}

var a = getAll();

// exit if features not supported
if (!a || !getFontSize(a[0]) || !a[0].style ) return;

var s, as, au, i = a.length;
while ( --i ) {
s = getFontSize(a[i]); // Get the font size
an = parseFloat(s); // Get the number part
au = s.replace(an,''); // Get the units
a[i].style.fontSize = an + factor + au;
}
setCookie("fontFactor", current)
}

//-------------------------------------------------
function getCookie(name) {
var dc = document.cookie;
var index = dc.indexOf(name + "=");
if (index == -1) return null;
index = dc.indexOf("=", index) + 1; // first character
var endstr = dc.indexOf(";", index);
if (endstr == -1) endstr = dc.length; // last character
return unescape(dc.substring(index, endstr));
}
//-------------------------------------------------
function setCookie(name, value)
{
document.cookie = name + "=" + escape(value);
}

// Get the factor from the cookie
var current = +getCookie("fontFactor");
if ( isNaN(current) ){
current = 0;
}


--
Rob
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chad Scharf | last post by:
I have a legacy ASP application running on IIS 6.0 (Windows Server 2003 Web Edition) that is throwing an error when processesing a certain asp page that has about 200 or so include directives. ...
0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
7
by: mescaline | last post by:
Hi, Suppose a_file.cpp contains a function a_function() Now to include it in main_file.cpp I just do #include "a_file.cpp" and I'm all set. i recently came across this seemingly roundabout...
6
by: atv | last post by:
Alright, i have some questions concerning include files en global variables.I hope someone is willing to answer these. 1).Why is it that if i define a global variable in a file, say main.c, and...
5
by: David Mathog | last post by:
One thing that can make porting C code from one platform to another miserable is #include. In particular, the need to either place the path to an included file within the #include statement or to...
1
by: Minh | last post by:
I've just installed VS.NET 2003 on my Athlon XP 1800+. However I couldn't get any project with STL includes to compile even if I create a new empty project (and added #include <string>). It gave me...
1
by: ya man | last post by:
when i use #include <iostream.h> in some files i get lots of error messages of the kind 'ambiguous symbol this is solved when i use #include <iostream why is that ? and can i use #include...
9
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
2
by: key9 | last post by:
Hi all look at the organize tree main.c ------ #include lib_adapter.c main() { foo();
16
by: Chris Shearer Cooper | last post by:
In our Visual Studio 2005 application, we have several of our application's H files that are #included into stdafx.h. What is odd, is that when we change those application H files, VS2005...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.