...

View Full Version : help with if then.



spicyfetus
01-20-2005, 05:43 PM
hey everyone. i dont even know if what im about to ask is even possible. but i really need some help here.

im trying to make an alternating link to either close the open window, or to send the browser back one page depending on if anything is in the browser's history or not.

im sure everyone knows the code for close and back...but here they are anyways just incase:

<a href="javascript:window.close();">Close Window</a>

<a href="javascript:history.back();">Back</a>

of course i am using a small image at the beginning of each string of code but im not sure that makes a difference.

anyways...what i need is an if then that can read the browser's history...using the history.length() i assume...and determine which code to place on the page based on that information.

here is what i have tried so far:

<sript language="Javascript">

if (history.length() == 0)
{
document.write("<a href="javascript:window.close();">Close Window</a>");
}

else {
document.write("<a href="javascript:history.back();">Back</a>");
}

</script>

and...

if (history.length() < 1)
{
document.write("<a href="javascript:window.close();">Close Window</a>");
}

else{
document.write("<a href="javascript:history.back();">Back</a>");
}

and...if tried alternating the if and else such as:

if (histoy.length() > 0)
{
document.write("<a href="javascript:history.back();">Back</a>");
}

else{
document.write("<a href-"javascript:window.close();">Close Window</a>");
}

if also tried all of those using document.writeln.

im at a complete stand still.

the page its going to be used on can be found at the following link:

http://melted-dreams.net/~spicyfetus/pages/404.htm

so...any help is greatly appreciate....thanks a ton guys and gals :)

_Aerospace_Eng_
01-20-2005, 06:12 PM
try this, put the following code in your head tags

<script type="text/javascript">
<!--
function newhistory(){
var times=history.length;
if (times == 0)
{
document.getElementById("altlink").innerHTML="<a href='javascript:window.close()'>Close Window</a>";
}

else {
document.getElementById("altlink").innerHTML="<a href='javascript:history.back()'>Back</a>";
}
}
//-->
</script>
add this onload code to your body tag

onload="newhistory()"
and where ever you want the link to appear put some kind of element there that has an id of altlink for example if i wanted a link to appear in a table i would do this

<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>some content here</td>
<td><span id="altlink"></span></td>
</tr>
</table>
hth

spicyfetus
01-20-2005, 06:56 PM
awesome...it works perfectly.

thanks for the help. im really gonna have to analyze your code here to see what you've done. really interesting stuff...it's alot more complex than i had expected it to be.

could you possibly explain a little of it?

_Aerospace_Eng_
01-20-2005, 07:11 PM
sure i'll try to explain it so u can understand it
here i am just defining the function and declaring a variable, times=history.length

<script type="text/javascript">
<!--
function newhistory(){
var times=history.length;
if (times == 0)
{
document.getElementById("altlink").innerHTML="<a href='javascript:window.close()'>Close Window</a>";
}

else {
document.getElementById("altlink").innerHTML="<a href='javascript:history.back()'>Back</a>";
}
}
//-->
</script>
here is the actual if else statement, its basically saying if history.length is equal to 0 then it will write the correct link, and else write the other link the one for history back, into an element that as an id of altlink, innerHTML is more dynamic than document.write but its not DOM compliant but its okay to use for what u are trying to do, u can't use quotes within the innerHTML quotes because that will tell the code that the line is done so u have to use single quotes like i did

if (times == 0)
{
document.getElementById("altlink").innerHTML="<a href='javascript:window.close()'>Close Window</a>";
}

else {
document.getElementById("altlink").innerHTML="<a href='javascript:history.back()'>Back</a>";
}
}
//-->
</script>
then i used the body onload to call the function once as the page loaded so it would write the correct link into the page, there are other ways to do what u want but this was the first way i came up with, u could even get rid of a line by changing this

var times=history.length;
if (times == 0)
to just simply this

if (history.length == 0)

spicyfetus
01-20-2005, 07:13 PM
awesome. i had tried using functions before. but i was still trying to hit around the history.length() for whatever reason...and i had never thought of using on load.

thanks so much for your help :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum