---
title: "layer problem"
type: "forum-thread"
url: "https://www.webforum.nu/amne/javascript/17951-layer-problem"
topic: "JavaScript"
topic_url: "https://www.webforum.nu/amne/javascript"
author: "OveRRidE"
published: "2001-07-06T07:21:00.000Z"
updated: "2001-07-06T12:14:00.000Z"
replies: 3
views: 169
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/javascript/17951-layer-problem"
---

# layer problem

## #1 — OveRRidE, 2001-07-06T07:21Z

Jag kan inte få lagret att visas.. 
 hur gör jag?

Kod i div.html:

```
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
	<title>Untitled</title>
<script language="javascript" src="js/script.js"></script>
</head>

<body>

<a href="#" onMouseUp="show('layer1')" onfocus="this.blur();">Klicka mig!</a><br>
<a href="#" onMouseUp="hide('layer1')" onfocus="this.blur();">Klicka mig2!</a>

<div id="layer1" style="position: absolute; visibility: hidden; left:100; top:100; width:400; height:150; z-index: 20">
Denna texten skall enbart synas när länken är aktiv.
</div>

</body>
</html>
```

Kod i script.js:

```
	<!--
	
	var isNav, isIE
	var coll = ""
	var styleObj = ""

	if (parseInt(navigator.appVersion) >= 4)
	{
		if (navigator.appName == "Netscape") {
			isNav = true
		} else {
			isIE = true
			coll = "all."
			styleObj = ".style"
		}
	}
	
	function getObject(obj) {
		var theObj		
		if (typeof obj == "string")	 {			
			theObj = eval("document." + coll + obj + styleObj)
		} else {
			theObj = obj
		}
		return theObj
	}
	
	function shiftTo(obj, x, y) {	
		var theObj = getObject(obj)		
		if (isNav) {
			theObj.moveTo(x, y)			
		} else{
			theObj.pixelLeft = x
			theObj.pixelTop = y
		}
	}
	
	function setZIndex(obj, zOrder) {
		var theObj = getObject(obj)
		theObj.zIndex = zOrder
	}
	
	function show(obj) {	
		var theObj = getObject(obj)
		hideAll()
		theObj.visibility = "visible"
	}
	
	function hide(obj) {
		var theObj = getObject(obj)
		theObj.visibility = "hidden"
	}
	
	function hideAll()
	{
		hide('layer1')
		hide('layer2')
		hide('layer3')
		hide('layer4')
		hide('layer5')
		hide('layer6')
		hide('layer7')
		hide('layer8')
		hide('layer9')
		hide('layer10')
		hide('layer11')
		hide('layer12')
		hide('layer13')
		hide('layer14')
		hide('layer15')
		hide('layer16')
		hide('layer17')
		hide('layer18')
		hide('layer19')
		hide('layer20')
	}
	//-->
```

Pliz someone.. jag får det inte o funka :(

Permalänk: https://www.webforum.nu/p/17951

## #2 — Jojoxx, 2001-07-06T11:13Z

Problemet är att du i functionen hideAll() försöker gömma lager som inte finns. Lägg till en funktion som ser ut så här;

```
function isObject(obj){
	if (document.all) {
		if(document.all[obj]){ return true; }
	} else if (document.layers){
		if(document.layers[obj]){ return true; }
	} else if (document.getElementById){
		if(document.getElementById(obj)){ return true; }
	}
}
```

och ändra sedan i functionen hideAll() så att det står;

```
if (isObject('layer1')){ hide('layer1'); }
if (isObject('layer2')){ hide('layer2'); }
if (isObject('layer3')){ hide('layer3'); }
...osv...
```

Permalänk: https://www.webforum.nu/p/333729

## #3 — OveRRidE, 2001-07-06T11:59Z

jo det funkar, men jag har problem med zIndex:en nu.. 

om jag först tar fram layer1 och sedan layer2, och sedam försöker ta fram layer1 igen, så visas den inte. Den hamnar väl bakom layer2 då antar jag.. 

Hur får jag den som klickas att alltid hamn högst upp?

kod för script.js:

```
	<!--	
	
	// Global Variables
	var isNav, isIE
	var coll = ""
	var styleObj = ""

	if (parseInt(navigator.appVersion) >= 4)
	{
		if (navigator.appName == "Netscape") {
			isNav = true
		} else {
			isIE = true
			coll = "all."
			styleObj = ".style"
		}
	}
	
	// Convert an objectname to a object reference
	function getObject(obj) {
		var theObj		
		if (typeof obj == "string")	 {			
			theObj = eval("document." + coll + obj + styleObj)
		} else {
			theObj = obj
		}
		return theObj
	}
	
	// Put an object on a specific co-ordinate
	function shiftTo(obj, x, y) {	
		var theObj = getObject(obj)		
		if (isNav) {
			theObj.moveTo(x, y)			
		} else{
			theObj.pixelLeft = x
			theObj.pixelTop = y
		}
	}
	
	// Set Z-order for object
	function setZIndex(obj, zOrder) {
		var theObj = getObject(obj)
		theObj.zIndex = zOrder
	}
	
	// Show object
	function show(obj) {	
		var theObj = getObject(obj)
		hideAll()
		theObj.visibility = "visible"
	}
	
	// Hide object
	function hide(obj) {
		var theObj = getObject(obj)
		theObj.visibility = "hidden"
	}
	
	function hideAll()
	{
		if (isObject('tjanst')){ hide('tjanst'); }
		if (isObject('kontakt')){ hide('kontakt'); }
		if (isObject('case')){ hide('case'); }
		if (isObject('start')){ hide('start'); }
		
	}
	//-->
	
	function isObject(obj){
	if (document.all) {
	if(document.all[obj]){ return true; }
	} else if (document.layers){
	if(document.layers[obj]){ return true; }
	} else if (document.getElementById){
	if(document.getElementById(obj)){ return true; }
	}
	}

	//-->
```

Länk:

```
<a href="#" onMouseUp="show('layer1');"><img src="gfx/h_tjanster.gif" border="0"></a>
```

Help..

Permalänk: https://www.webforum.nu/p/333730

## #4 — OveRRidE, 2001-07-06T12:14Z

Ups!

Jag kom på varför det inte funkade \*dunka hammare i huvet\*

```
function hideAll()
	{
		if (isObject('layer1')){ hide('layer1'); }
		if (isObject('layer2')){ hide('layer2'); }
		if (isObject('layer3')){ hide('layer3'); }
		if (isObject('layer4')){ hide('layer4'); }
		
	}
```

sådär! de skall ju heta rätt med :)

Permalänk: https://www.webforum.nu/p/333731

---

Tråden på webben: https://www.webforum.nu/amne/javascript/17951-layer-problem
