Forum Moderators: open

Message Too Old, No Replies

Javascript Popup

         

andrewsmd

1:27 pm on Apr 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a page that shows a div with a form when you click on a link. My problem is, whenever you click on one of the links it goes back to the top of the page. Does anyone know how I can keep my current spot in the page?

Here is my JS
function show(divID){

var tDiv = document.getElementById(divID);
tDiv.style.display="block";
}//show

function hide(divID){

var tDiv = document.getElementById(divID);
tDiv.style.display="none";
}//show

Here is my HTML
<a href = "#" onclick="show('{showDivID}')">New Job</a>
<div id = "div53" class = "newDiv" style="display: none;">
<input type = "submit" name = "newSubmit" value = "submit" onclick="hide('div53')">
</div>

And here is my css

.newDiv{
background-image: url("images/br_paper.jpg");
position: absolute;
left: 30%;
height: 30em;
width: 30em;
border: grey groove 3px;
z-index: 9;
}

I tried using anchors, but then it still jumps around the page even if they are placed right. I also tried taking out the # but then it just displays really fast and then disappears again. Any ideas? Thanks,

rocknbil

2:39 pm on Apr 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does anyone know how I can keep my current spot in the page?

Many form or page elements have a natural action. For hyperlinks, it's navigate, for forms, it's submit the form. To tell the browser to let JS manage the action and not perform the natural action, return false from your function.

This has the added benefit of giving you a way to make the form/link work if Javascript is disabled. If JS is disabled, it won't "get" the return false and revert to the natural action, allowing users to access your content whether or not JS is enabled.

In your example (in which I've economized the show/hide into one function:):


function showHide(divID){
var tDiv document.getElementById(divID);
tDiv.style.display = (tDiv.style.display=='block')?'none':'block';
return false;
}

<a href="link_to_alternate_content.html" onclick="return showHide('div1')">New Job</a>

andrewsmd

5:15 pm on Apr 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're function doesn't work in firefox. As soon as I return false, it just doesn't show anything when I click the link. Also, I should mention that using IE the page displays fine with anchors, it's just FF where it moves to that anchor. Any other ideas?

rocknbil

5:24 pm on Apr 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah. Put the = after var tDiv. My typo would have been (painfully) obvious using the FF Error Console.

Working example, an exact duplicate of the solution posed in another thread [webmasterworld.com] sans the unnecessary window.onload()!:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/1998/REC-html40-19980424/loose.dtd">
<!-- doctype on one line -->
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<style type="text/css">
.hideme { display: none; }
</style>
<script type="text/javascript">
function showHide(divID){
var tDiv = document.getElementById(divID);
tDiv.style.display = (tDiv.style.display=='block')?'none':'block';
return false;
}
</script>
</head>
<body>
<p><a href="link_to_alternate_content.html" onclick="return showHide('div1')">New Job</a> </p>
<p><a href="link_to_alternate_content2.html" onclick="return showHide('div2')">Old Job</a> </p>
<p><a href="link_to_alternate_content3.html" onclick="return showHide('div3')">Bad Job</a> </p>
<p id="div1" class="hideme">Div 1 content goes here.</p>
<p id="div2" class="hideme">Div 2 content goes here.</p>
<p id="div3" class="hideme">Div 3 content goes here.</p>
</body>
</html>

andrewsmd

6:58 pm on Apr 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks how do you use the FF error console?