Solved

make link active on page load

Posted on 2011-03-03
2
329 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
Comment Utility

// Untested but try something like this:

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

Author Closing Comment

by:johnm07
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

771 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

12 Experts available now in Live!

Get 1:1 Help Now