Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catching event firing

Status
Not open for further replies.

darkling235

Programmer
Oct 30, 2006
24
US
Hello. I'm trying to do two things when a user right clicks on my document. Number 1 I need to display a special context menu, I have this part written. Number two, I need to be able to identify what text chunk (the node would probably work) the user did click on and put it in a variable.

This is my menu code. Parts of it use JSP tags. Can anyone tell me how to alter it to get the event firer? Thx a ton


<div id="ie5menu" class="skin0" onMouseover="highlightie5(event)" onMouseout="lowlightie5(event)" onMouseDown="mine(event)" display:none>
<%
for(int x = 0; x < str.length; x++)
out.println("<div class=\"menuitems\" url=\"" + str[x] + "\">" + str[x] + "</div>");
%>
</div>

<script language="JavaScript1.2">

//set this variable to 1 if you wish the URLs of the highlighted menu to be displayed in the status bar
var display_url=0

var ie5=document.all&&document.getElementById
var ns6=document.getElementById&&!document.all
if (ie5||ns6)
var menuobj=document.getElementById("ie5menu")

function showmenuie5(e){
//Find out how close the mouse is to the corner of the window
var rightedge=ie5? document.body.clientWidth-event.clientX : window.innerWidth-e.clientX
var bottomedge=ie5? document.body.clientHeight-event.clientY : window.innerHeight-e.clientY

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<menuobj.offsetWidth)
//move the horizontal position of the menu to the left by it's width
menuobj.style.left=ie5? document.body.scrollLeft+event.clientX-menuobj.offsetWidth : window.pageXOffset+e.clientX-menuobj.offsetWidth
else
//position the horizontal position of the menu where the mouse was clicked
menuobj.style.left=ie5? document.body.scrollLeft+event.clientX : window.pageXOffset+e.clientX

//same concept with the vertical position
if (bottomedge<menuobj.offsetHeight)
menuobj.style.top=ie5? document.body.scrollTop+event.clientY-menuobj.offsetHeight : window.pageYOffset+e.clientY-menuobj.offsetHeight
else
menuobj.style.top=ie5? document.body.scrollTop+event.clientY : window.pageYOffset+e.clientY

menuobj.style.visibility="visible"
return false
}

function hidemenuie5(e){
menuobj.style.visibility="hidden"
}

function highlightie5(e){
var firingobj=ie5? event.srcElement : e.target
if (firingobj.className=="menuitems"||ns6&&firingobj.parentNode.className=="menuitems"){
if (ns6&&firingobj.parentNode.className=="menuitems") firingobj=firingobj.parentNode //up one node
firingobj.style.backgroundColor="highlight"
firingobj.style.color="white"
if (display_url==1)
window.status=event.srcElement.url
}
}

function lowlightie5(e){
var firingobj=ie5? event.srcElement : e.target
if (firingobj.className=="menuitems"||ns6&&firingobj.parentNode.className=="menuitems"){
if (ns6&&firingobj.parentNode.className=="menuitems") firingobj=firingobj.parentNode //up one node
firingobj.style.backgroundColor=""
firingobj.style.color="black"
window.status=''
}
}

function jumptoie5(e){
var firingobj=ie5? event.srcElement : e.target
if (firingobj.className=="menuitems"||ns6&&firingobj.parentNode.className=="menuitems"){
if (ns6&&firingobj.parentNode.className=="menuitems") firingobj=firingobj.parentNode
if (firingobj.getAttribute("target"))
window.open(firingobj.getAttribute("url"),firingobj.getAttribute("target"))
else
window.location=firingobj.getAttribute("url")
}
}

if (ie5||ns6){
menuobj.style.display=''
document.oncontextmenu=showmenuie5
document.onclick=hidemenuie5
}

</script>
 
Ok sorry to be a pest but I'm still not grasping the soultion after reading that tutorial. Every property and function I try to use to catch the "source" (the actual text node that was right clicked on) it comes back undefined. I get the firer sort of from calling

event.target which gives me back the HTMLTableObject but I can;t seem to access the text or any other type of node. Can somebody help me?
Thank You
 
Ok heres the whole html file. There is a lot of JSP in it but the basic idea is that I'm catching the event when someone right clicks but then I can't determine what node fired the event. Help would be greatly appreciated.
Thanks

<%@ page import = "com.java.activityinfo.template.*" %>
<%@ page import = "Template.Template" %>
<%@ page import = "Template.URLInterface" %>
<%@ page import = "java.util.*;" %>

<%!URLInterface uri;%>
<%uri = new URLInterface(); %>
<%!String[] str;%>
<%!Template t;%>

<%
t = (Template) session.getAttribute("template");
Vector v = (Vector)t.getTags();

str = new String[v.size()];

for(int x = 0; x < v.size(); x++)
str[x] = (String)v.elementAt(x);

%>

<HTML>
<HEAD>
<style>
<!--

/* Context menu Script- © Dynamic Drive ( Last updated: 01/08/22
For full source code and Terms Of Use, visit */

.skin0{
position:absolute;
width:165px;
border:2px solid black;
background-color:menu;
font-family:Verdana;
line-height:20px;
cursor:default;
font-size:14px;
z-index:100;
visibility:hidden;
}

.menuitems{
padding-left:10px;
padding-right:10px;
}
-->
</style>

<script language="JavaScript1.2">

window.onerror = function(msg, err_url, line) {
alert( msg + ' error occured on line: ' + line);
}


function refresh()
{
setTimeout("window.location = '<%= uri.getBaseURL(request)%>/refreshxml'",3000);


}

function send(num)
{
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari


if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', "<%= uri.getBaseURL(request)%>/remove", true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-
self.xmlHttpReq.send(num);
}







</script>
</HEAD>
<BODY onLoad="javascript:refresh();">

<div id="ie5menu" class="skin0" onMouseover="highlightie5(event)" onMouseout="lowlightie5(event)" onMouseDown="mine(event)" display:none>
<%
for(int x = 0; x < str.length; x++)
out.println("<div class=\"menuitems\" url=\"" + str[x] + "\">" + str[x] + "</div>");
%>
</div>

<script language="JavaScript1.2">

//set this variable to 1 if you wish the URLs of the highlighted menu to be displayed in the status bar
var display_url=0

var ie5=document.all&&document.getElementById
var ns6=document.getElementById&&!document.all
if (ie5||ns6)
var menuobj=document.getElementById("ie5menu")

function showmenuie5(e){
//Find out how close the mouse is to the corner of the window
var rightedge=ie5? document.body.clientWidth-event.clientX : window.innerWidth-e.clientX
var bottomedge=ie5? document.body.clientHeight-event.clientY : window.innerHeight-e.clientY

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<menuobj.offsetWidth)
//move the horizontal position of the menu to the left by it's width
menuobj.style.left=ie5? document.body.scrollLeft+event.clientX-menuobj.offsetWidth : window.pageXOffset+e.clientX-menuobj.offsetWidth
else
//position the horizontal position of the menu where the mouse was clicked
menuobj.style.left=ie5? document.body.scrollLeft+event.clientX : window.pageXOffset+e.clientX

//same concept with the vertical position
if (bottomedge<menuobj.offsetHeight)
menuobj.style.top=ie5? document.body.scrollTop+event.clientY-menuobj.offsetHeight : window.pageYOffset+e.clientY-menuobj.offsetHeight
else
menuobj.style.top=ie5? document.body.scrollTop+event.clientY : window.pageYOffset+e.clientY

menuobj.style.visibility="visible"
return false
}

function hidemenuie5(e){
menuobj.style.visibility="hidden"
}

function highlightie5(e){
var firingobj=ie5? event.srcElement : e.target
if (firingobj.className=="menuitems"||ns6&&firingobj.parentNode.className=="menuitems"){
if (ns6&&firingobj.parentNode.className=="menuitems") firingobj=firingobj.parentNode //up one node
firingobj.style.backgroundColor="highlight"
firingobj.style.color="white"
if (display_url==1)
window.status=event.srcElement.url
}
}

function lowlightie5(e){
var firingobj=ie5? event.srcElement : e.target
if (firingobj.className=="menuitems"||ns6&&firingobj.parentNode.className=="menuitems"){
if (ns6&&firingobj.parentNode.className=="menuitems") firingobj=firingobj.parentNode //up one node
firingobj.style.backgroundColor=""
firingobj.style.color="black"
window.status=''
}
}

function jumptoie5(e){
var firingobj=ie5? event.srcElement : e.target
if (firingobj.className=="menuitems"||ns6&&firingobj.parentNode.className=="menuitems"){
if (ns6&&firingobj.parentNode.className=="menuitems") firingobj=firingobj.parentNode
if (firingobj.getAttribute("target"))
window.open(firingobj.getAttribute("url"),firingobj.getAttribute("target"))
else
window.location=firingobj.getAttribute("url")
}
}

if (ie5||ns6){
menuobj.style.display=''

//document.oncontextmenu=showmenuie5

document.oncontextmenu=myFunction

document.onclick=hidemenuie5
}


//This is the function i'm trying to fix
function myFunction(e)
{
var elem = e.target;
alert(elem.originalTarget);
}


</script>

<%

PageTemplate template = (PageTemplate)session.getAttribute("temp");
PageElement[] temp = template.getElements();
out.println("NAME: " + t.getLasturl());
out.println("<BR><BR><BR><BR>");

out.println("<TABLE>");
for(int x = 0; x < temp.length; x++)
{
out.println("<TR onclick=send(" + x + ")>");
out.println("<TD>" + temp[x].getText() + "</TD>");
out.println("</TR>");
out.println("<TR><TD>Type: " + temp[x].getType() + "</TD></TR>");
out.println("<TR><TD></TD> </TR>");
out.println("<TR><TD></TD> </TR>");
out.println("<TR><TD></TD> </TR>");
out.println("<TR><TD></TD> </TR>");
out.println("<TR><TD></TD> </TR>");
}
out.println("</TABLE>");


%>




</BODY>
</HTML>
 
And where is the client-side code that Cory asked for? You provided the server-side scripting instead. In the browser, click on View->Source, then copy and paste THAT in here.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top