开发者

problem with javascript with JSP

开发者 https://www.devze.com 2023-02-23 01:59 出处:网络
Hey all i am trying to use javascript with my jsp file as under: JSP File: <?xml version=\"1.0\" encoding=\"UTF-8\" ?>

Hey all i am trying to use javascript with my jsp file as under:

JSP File:

 <?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>title goes here</title>
<link rel="icon" href="scripts/assets/favicon.ico" type="image/x-icon"/>
<%-- 
    Ajax code to refresh the main page's contents.
    This ajax code is specific to the home page not 
    for all so it is kept outside the main script file.
 --%>
<script type="text/javascript" src="scripts/AjaxRefresh.js"></script>
<script type="text/javascript" src="scripts/MainScript.js"></script>
<link rel="stylesheet" type="text/css" href="scripts/MainStyle.css" />

</head>
<body>
<div class="mainContainer">
<div class="header">
header
</div>
<div class="leftNavigation">
<a href="Home" title="Go to Home Page" class="navigationButton" id="home" onclick="animateLink()">Home</a>
<br/>
<a href="Home" title="Go to Home Page" class="navigationButton">Home</a>
<br/>
<a href="Home" title="Go to Home Page" class="navigationButton">Home</a>
<br/>
<a href="Home" title="Go to Home Page" class="navigationButton">Home</a>
<开发者_Go百科;br/>
<a href="Home" title="Go to Home Page" class="navigationButtonActive">Home</a>
<br/>
<a href="Home" title="Go to Home Page" class="navigationButton">Home</a>
<br/>
<a href="Home" title="Go to Home Page" class="navigationButton">Home</a>
<br/>
<a href="Home" title="Go to Home Page" class="navigationButton">Home</a>
</div>
<div class="mainContentArea">
Main Content Area
</div>
<div class="rightTabBar">
Right Tab Bar
</div>
<div class="footer">
Footer
</div>
</div>
</body>
</html>

and the MainScript.js file is as under:

window.onload = initAll();

function initAll(){     
    var navigationButton = document.getElementsByTagName("a");
    for ( var int = 0; int < navigationButton.length; int++) {
        if(navigationButton[int].className == "navigationButton")
            navigationButton[int].onclick = animateLink;
    }   
}

function animateLink(){
    this.className = "navigationButtonActive";
    return false;
}

but when i tried to execute this code i found that javascript code is not working properly and with firebug i found the variable navigationBUtton is an empty array.

Actually i am from PHP background so don't know precisely the concepts of jsp so what sort of problem here is??

PS: I am using eclipse 3.5 with apache tomcat6 as web server in ubuntu 10.10 platform.

thanks :)


You're assigning the outcome of the function to the window.onload instead of letting it point to a function name.

Replace

window.onload = initAll();

by

window.onload = initAll;

or just do

window.onload = function() {     
    var navigationButton = document.getElementsByTagName("a");
    for ( var int = 0; int < navigationButton.length; int++) {
        if(navigationButton[int].className == "navigationButton")
            navigationButton[int].onclick = animateLink;
    }   
}

See also:

  • window.onload documentation in MDC

Note that this problem is unrelated to JSP.

0

精彩评论

暂无评论...
验证码 换一张
取 消