Solved

make link active on page load

Posted on 2011-03-03
2
330 Views
Last Modified: 2012-05-11
I recently received help here with using java script to handle different link states as well as replacing content in a targeted div id.

My current problem is upon initial page load none of the links are active therefore if a user clicks any link other then the top link (associated with the primary div content) the new content is rendered outside the targeted div. This of course screws up the whole page layout. (see attached image)

Question: How can I set the <id=container1> link to be active upon page load?  
Or is there a better way to fix this problem

The attached code is what I am using within my site.
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)">
    <meta name="created" content="Wed, 09 Feb 2011 02:03:23 GMT">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <style>
   body { background-color: #FFFFFF }

/* navigation */
#navigation {width: 102px; height:129; margin: 0 auto; }

ul#navigation { list-style: none; }
              ul#navigation li {display: block;} 
                     ul#navigation li a { 
                            text-indent: -9999px;
                            display: block;
                            width: 102px;
                            height: 129px;
                            align: left;
                            /*float: left; */
                            }
ul#navigation li a.home {
              width: 102px;
}
                            
ul#navigation li a.about {
              width: 102px;
              }
              
ul#navigation li a.contact {
              width: 102px;
              }
              
ul#navigation li a.home:hover, 
ul#navigation li a.about:hover, 
ul#navigation li a.contact:hover 
{
    background-position: center right;
   }           
 
.normal { background: url("phoneicon.png") no-repeat scroll center left; }
.clicked { background: url("phoneicon.png") no-repeat scroll center right;}

.content {
  margin-top: 20px; 
  padding:10px; 
  text-align:left
}    
</style>
    <title>button test 2</title>
	
<script type="text/javascript"><!--
var saveLink = null;
var saveContainer = null;	
function ReplaceContentInContainer(link) {
  var container = document.getElementById(link.id.split('_')[0]); // passing the id, split on _link
  if (saveLink) saveLink.className=saveLink.className.replace(/ clicked/," normal");
  if (saveContainer) saveContainer.style.display='none';  
  link.className = link.className.replace(/ normal/," clicked");
  container.style.display=''; // shows the container
  saveLink=link;
  saveContainer = container;
  link.blur(); // remove the border
  return false
}
//--></script>
</head>
<body id="page1">
<br /> 
<div class="content" id="container1">
	Original text for <b>Home</b>
</div>
<div class="content" id="container2" style="display:none">
	Original text for <b>about</b>
</div>
<div class="content" id="container3" style="display:none">
	Original text for <b>contact</b>
</div>
<ul id=navigation>
	<li><a class="home normal" href="#" onClick="return ReplaceContentInContainer(this)" title"home" id="container1_link"></a></li>
	<li><a class="about normal" href="#" onClick="return ReplaceContentInContainer(this)" title"about" id="container2_link"></a></li>
	<li><a class="contact normal" href="#" onClick="return ReplaceContentInContainer(this)" title"contact" id="container3_link"></a></li>
	<!-- end of ul id=navigation --></ul>
  </body>
</html>

Open in new window

image of problem with explainations
0
Comment
Question by:johnm07
2 Comments
 
LVL 20

Accepted Solution

by:
Proculopsis earned 500 total points
ID: 35034173

// Untested but try something like this:

window.onload = function() {
  var fake = document.getElementById( "container1_link" );
  ReplaceContentInContainer( fake );
}
0
 

Author Closing Comment

by:johnm07
ID: 35059266
I've added your code at the end of the current JavaScript. The result is upon page load the primary link is now active. All other button link selections fill the targeted div as they should.

Great work , thank you for the code and fast response.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now