PDA

View Full Version : using result of one function in another



n3wb
Nov 20th, 2009, 03:29 AM
I will tell you now that I am not a javascript programmer and this is a very basic question. Please don't flame.

I have been writing a web site and have found 2 functions that work and do what I want them to do, but I want to combine them. The first function will grab the window size.(it actually prints it out to the screen and I know how to get that to stop by taking out the last 2 lines) What I want to do with the results from the function alertSize() is exactly what the function res() does though. I want it to take the window size and jump to the appropriate web directory. What I don't know is if I can write that all as one function or if I have to pass the results of function alertSize() to function res(). Or how to do it for that matter, I don't even know what this is called in programming lingo. I am not asking for an answer here I would just like someone to point me in the right direction that I may learn a little more about what it is I want to do so I can get this to work. Giving me a search term would be a BIG help.

Here is the first function.

// <script language="JavaScript"> //-->
function alertSize() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
window.alert( 'width = ' + myWidth );
window.alert( 'height = ' + myHeight );
}
// </script> //-->

and the second function.


// <script language="JavaScript"> //-->
function res()
{
alert('Screen Resolution Is '+screen.width+' by '+screen.height);
}
if ((screen.width>=1280) && (screen.height>=1024))
{
window.location="/12/index.html";
}
else if ((screen.width>=1024) && (screen.height>=768))
{
window.location="/10/index.html";
}
else if ((screen.width>=800) && (screen.height>=600))
{
window.location="lowres1.html";
}
else if ((screen.width>=640) && (screen.height>=480))
{
window.location="lowres2.html";
}
else
{
window.location="lowres3.html";
}
// </script> //-->

Old Pedant
Nov 20th, 2009, 03:38 AM
<script type="text/javascript">
function alertSize()
{
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
if (myWidth>=1280 && myHeight>=1024) location.href="/12/index.html";
if (myWidth>=1024 && myHeight>=768) location.href="/10/index.html";
if (myWidth>=800 && myHeight>=600) location.href="lowres1.html";
if (myWidth>=640 && myHeight>=480) location.href="lowres2.html";
location.href="lowres3.html";
}
</script>

Old Pedant
Nov 20th, 2009, 03:38 AM
But don't know why you would still be worrying about IE 4 compatibility.

n3wb
Nov 20th, 2009, 04:22 AM
But don't know why you would still be worrying about IE 4 compatibility.

I just used scripts I had pulled off of google, as I am not worried about IE 4 compatibilty but hey if it's in their it can hurt can it? Thank you for the code. Seems that no matter what the window size is though it jumps to lowres3.html What confuses me here is that you used the 4 if statements at the bottom instead of else if statements.

blue642
Nov 20th, 2009, 04:53 AM
I'm still very new to javascript myself, but I believe the point of just using the if statements is because if the condition is true, it will be loading a new page anyway, and the rest of the script will no longer exist to run.

I don't know of a fix for the lowres3 default other than maybe just copying and pasting the else if statements from your res() and using those to replace the if statements that old pedant made.... of course their may be a better solution (if that even works)

Old Pedant
Nov 20th, 2009, 06:28 AM
I'm still very new to javascript myself, but I believe the point of just using the if statements is because if the condition is true, it will be loading a new page anyway, and the rest of the script will no longer exist to run.
100% correct. Or at least that's what I thought.

Given what he's seeing, maybe he does need the else's.

No time to try it out now.

But why not debug it??

What *are* the values of myHeight and myWidth?

alert() them and see if the subsequent if statements make sense.

Old Pedant
Nov 20th, 2009, 06:29 AM
Since it can't hurt, why not try leaving in the else's?


if (myWidth>=1280 && myHeight>=1024) location.href="/12/index.html";
else if (myWidth>=1024 && myHeight>=768) location.href="/10/index.html";
else if (myWidth>=800 && myHeight>=600) location.href="lowres1.html";
else if (myWidth>=640 && myHeight>=480) location.href="lowres2.html";
else location.href="lowres3.html";

n3wb
Nov 20th, 2009, 06:55 AM
I'm still very new to javascript myself, but I believe the point of just using the if statements is because if the condition is true, it will be loading a new page anyway, and the rest of the script will no longer exist to run.

I don't know of a fix for the lowres3 default other than maybe just copying and pasting the else if statements from your res() and using those to replace the if statements that old pedant made.... of course their may be a better solution (if that even works)

Thanks. I did find the correct syntax to achieve what I was looking for. The script continues to run even after it jumps to the correct page.
I use this in my pages head section


<script src="/css/window.js" type="text/javascript" charset="utf-8"></script>

I call the code with


<body class="home" onLoad='alertSize()'>

and use this for the finished code inside the window.js file.


// <script type="text/javascript"> //-->
function alertSize()
{
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
if ((myWidth>=1260) && (myHeight>=780)) location.href="/12/index.html";
else if ((myWidth>=1000) && (myHeight>=550)) location.href="/10/index.html";
else if ((myWidth>=780) && (myHeight>=375)) location.href="lowres1.html";
else if ((myWidth>=620) && (myHeight>=300)) location.href="lowres2.html";

window.alert( 'width = ' + myWidth );
window.alert( 'height = ' + myHeight );
}
// </script> //-->

If you run the code you find that the width and height are still posted to the screen(because of the window.alerts) even after the correct page continues to load, so the script will run all the way through. But it does function correctly. Thanks again to Old Pendant and blue642 for helping out.