Forum Moderators: open

Message Too Old, No Replies

javascript error

         

gonny

6:14 pm on Dec 18, 2007 (gmt 0)

10+ Year Member





<html>

<head>
<title>Menu test </title>
<script type="text/javascript" src="javamenu.js"></script>

</head>

<body bgcolor="white" text="black" link="blue" vlink="purple" alink="red">
<p>&nbsp;</p>

<a href='#' onclick="vp_Show_Menu('veriu_menu1', this);return false;"><span id="vp_menu_container">Menu test</span></a>

<div id="veriu_menu1" style="display:none">

<table cellpadding="4" cellspacing="1" border="0">

<tr><td style="text-align: left;">Demo Sites</td></tr>
<tr><td style="font-weight: normal; text-align: left;"><a href="#">test1</a></td></tr>
<tr><td style="font-weight: normal; text-align: left;"><a href="#">test2</a></td></tr>
<tr><td style="font-weight: normal; text-align: left;"><a href="#">test3</a></td></tr>

</table>
</div>
</body>

</html>


javamenu.js



var agt = navigator.userAgent.toLowerCase();
var agt_ver = parseInt(navigator.appVersion);
var is_mozilla = (navigator.product == "Gecko");
var is_opera = (agt.indexOf("opera")!= -1);
var is_konqueror = (agt.indexOf("konqueror")!= -1);
var is_webtv = (agt.indexOf("webtv")!= -1);
var is_ie = ((agt.indexOf("msie")!= -1) && (!is_opera) && (!is_webtv));
var is_netscape = ((agt.indexOf("compatible") == -1) && (agt.indexOf("mozilla")!= -1) && (!is_opera) && (!is_webtv));
var is_win = (agt.indexOf("win"!= -1));
var is_mac = (agt.indexOf("mac")!= -1);

var vp_active_Menu = false;

function vp_Show_Menu(id,inputObj)
{

if (vp_active_Menu && vp_active_Menu.style.visibility == 'visible') {
vp_active_Menu.style.visibility='hidden';
} else {
vp_Menu_Show(id,inputObj);
}
}

function vp_Menu_Show(id,inputObj)
{

var leftPos = (vp_Menu_getLeftPos(inputObj) );
var topPos = vp_Menu_getTopPos(inputObj);
vp_active_Menu = document.getElementById(id);

vp_active_Menu.style.position = 'absolute';
vp_active_Menu.style.top = topPos + 20 + "px";
vp_active_Menu.style.left = leftPos + "px";
vp_active_Menu.style.zIndex = '50';
vp_active_Menu.style.display = 'block';
vp_active_Menu.style.visibility='visible';

//vp_active_Menu.onclick = function () {vp_Menu_Hide();}
}

function vp_Menu_Hide(event) {

if (is_ie)
el = window.event.srcElement.id;
else
el = event.target.id;

if (el!= 'vp_menu_container' && vp_active_Menu.style.visibility == 'visible') {
vp_active_Menu.style.visibility = "hidden";
}

}

function vp_Menu_getTopPos(inputObj)
{
var returnValue = inputObj.offsetTop;
while((inputObj = inputObj.offsetParent)!= null){
if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
}
return returnValue;
}

function vp_Menu_getLeftPos(inputObj)
{
var returnValue = inputObj.offsetLeft;
while((inputObj = inputObj.offsetParent)!= null){
if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
}
return returnValue;
}

if (!document.addEventListener){
document.attachEvent("onclick", vp_Menu_Hide);
} else {
document.addEventListener("click", vp_Menu_Hide, false);
}


the problem is on "function vp_Menu_Hide(event) {"
if I click somewhere in page first time and not in link of menu I get an error. on This line:

if (el!= 'vp_menu_container' && vp_active_Menu.style.visibility == 'visible') {
vp_active_Menu.style.visibility = "hidden";
}

How I can solve this? can help any one?

Dabrowski

7:19 pm on Dec 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It would help if you actually told us what the error was, and when it occurred? On page load? On hover? On click?

You can't just post your entire page and say 'Here, I messed it up, you fix it'.

Trace

7:38 pm on Dec 18, 2007 (gmt 0)

10+ Year Member



Dabrowski, he did say this...
the problem is on "function vp_Menu_Hide(event) {"
if I click somewhere in page first time and not in link of menu I get an error. on This line:

if (el!= 'vp_menu_container' && vp_active_Menu.style.visibility == 'visible') {
vp_active_Menu.style.visibility = "hidden";
}

How I can solve this? can help any one?

At first glance it appears vp_active_Menu is empty, just add a validation to make sure it exists.

Use this instead of what you currently have:
if(vp_active_Menu){
if (el!= 'vp_menu_container' && vp_active_Menu.style.visibility == 'visible') {
vp_active_Menu.style.visibility = "hidden";
}
}

gonny

7:39 pm on Dec 18, 2007 (gmt 0)

10+ Year Member



thanx Trace

sorry for my bad english

Dabrowski

8:28 pm on Dec 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh I'm so sorry gonny, must have read it a bit too quickly, just skimmed over that and thought it part of the code!

I take back my previous comment. If Trace's suggestion doesn't fix it please post back and we'll have another go!