?
Solved

make link active on page load

Posted on 2011-03-03
2
Medium Priority
?
336 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 20

Accepted Solution

by:
Proculopsis earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

764 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