Can somebody help with this javascript please ?

R

Roopa

Hi all

I am not so good at javascript. Can anybody tell me debug this ?

This is a program which calls otherpage with a variable and the layer div1
should be hidden or shown depending on the variable I am sending across to
the otherpage.htm

firstpage.htm
==========

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>first page</title>
</head>

<body>
<a href="otherpage.htm?vis=yes">Get other page</a>
</body>

</html>



Otherpage.htm
===========

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>otherpage</title>
<script language="JavaScript"><!--
var vis = qsobj(0)
var param = 'div1';

function qsobj(parm)
{
var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\+/g," ")) : null
}

function disappear(param)
{
document.getElementById(param).style.visibility = 'hidden';
// param.style.visibility="hidden";
}

function reappear()
{
document.getElementById(param).style.visibility = 'visible';
//param.style.visibility="visible";
}

if (vis == 'yes'){
reappear('div1')
}
else {
disappear('div1')
}

//--></script>
</head>

<body>
Hello
<div style="position: absolute; width: 100px; height: 34px; z-index: 1;
left: 11px; top: 60px; visibility:hidden" id="div1">
Hidden layer</div>
</body>

</html>



Thanks,
 
K

Kevin Spencer

I am not so good at javascript. Can anybody tell me debug this ?

debug this.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.
 
K

Kevin Spencer

You had a few problems. The biggest one was that you were running a script
in the head of the document that referred to elements in the body. When the
document loads, the elements in the body are not loaded when the script
runs. I moved the "on load" script to a position below the element it
references.

Read the code to see the other errors I corrected.

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>otherpage</title>
<script language="JavaScript"><!--
var vis = qsobj(0)
var param = 'div1';

function qsobj(parm)
{
var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=");
return qvbl[1] == "yes" ? unescape(qvbl[1].replace(/%20|\+/g," ")) : null
}

function disappear(param)
{
document.getElementById(param).style.visibility = 'hidden';
}

function reappear()
{
document.getElementById(param).style.visibility = 'visible';
}
//--></script>
</head>

<body>
Hello
<div style="position: absolute; width: 100px; height: 34px; z-index: 1;
left: 11px; top: 60px; visibility:hidden" id="div1">
Hidden layer</div>
<script type="text/javascript"><!--
if (vis == 'yes') reappear('div1');
else disappear('div1');
// --></script>
</body>

</html>

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

Roopa said:
Hi all

I am not so good at javascript. Can anybody tell me debug this ?

This is a program which calls otherpage with a variable and the layer div1
should be hidden or shown depending on the variable I am sending across to
the otherpage.htm

firstpage.htm
==========

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>first page</title>
</head>

<body>
<a href="otherpage.htm?vis=yes">Get other page</a>
</body>

</html>



Otherpage.htm
===========

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>otherpage</title>
<script language="JavaScript"><!--
var vis = qsobj(0)
var param = 'div1';

function qsobj(parm)
{
var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\+/g," ")) : null
}

function disappear(param)
{
document.getElementById(param).style.visibility = 'hidden';
// param.style.visibility="hidden";
}

function reappear()
{
document.getElementById(param).style.visibility = 'visible';
//param.style.visibility="visible";
}

if (vis == 'yes'){
reappear('div1')
}
else {
disappear('div1')
}

//--></script>
</head>

<body>
Hello
<div style="position: absolute; width: 100px; height: 34px; z-index: 1;
left: 11px; top: 60px; visibility:hidden" id="div1">
Hidden layer</div>
</body>

</html>



Thanks,
 
T

Trevor L.

Another way to write the [dis]appear function is
function appear(param)
{
e = document.getElementById(param).style
e.visibility = (e.visibility != 'visible') ? 'visible' : 'hidden
}

You then only have to call the one function:
<script type="text/javascript">appear('div1')</script>

Not tested but I use a similar function (which works) and I copied it from
that.
 
K

Kevin Spencer

Hi Trevor,

True, but I didn't want to make assumptions about his requirements. I also
noted to myself that a single function which took a boolean parameter would
have worked, but if he was planning on extending these functions to do more,
I shouldn't have made the assumption that he needed a single function. So I
didn't!

Anyway, that's my story, and I'm stickin' to it! ;-)

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.
 
R

Roopa

Thanks Kevin and Trevor. Your inputs were really helpful.

Kevin Spencer said:
Hi Trevor,

True, but I didn't want to make assumptions about his requirements. I also
noted to myself that a single function which took a boolean parameter would
have worked, but if he was planning on extending these functions to do more,
I shouldn't have made the assumption that he needed a single function. So I
didn't!

Anyway, that's my story, and I'm stickin' to it! ;-)

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

Trevor L. said:
Another way to write the [dis]appear function is
function appear(param)
{
e = document.getElementById(param).style
e.visibility = (e.visibility != 'visible') ? 'visible' : 'hidden
}

You then only have to call the one function:
<script type="text/javascript">appear('div1')</script>

Not tested but I use a similar function (which works) and I copied it from
that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top