Solved

Calling a javascript function from flash

Posted on 2010-11-10
17
444 Views
Last Modified: 2012-05-10
Bellow is some JavaScript that I'm using for some drop-down menus.
http://www.leigeber.com/2008/11/drop-down-menu/

every thing is working as desired with the static version.

I would like to be able to call the onmouseover and onmouseout functions from Flash or other links on the page.

I'm not that familiar with JavaScript but it looks to me like there are new functions being created for each list element at run-time. How would i create over and out functions for the specific menu elements that i could call from anywhere in the page?

<script type="JavaScript">
var menu=function(){
	var t=15,z=50,s=6,a;
	function dd(n){this.n=n; this.h=[]; this.c=[]}
	dd.prototype.init=function(p,c){
		a=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
		clearInterval(c.t); c.style.overflow='hidden';
		if(f){
			p.className+=' '+a;
			if(!c.mh){c.style.display='block'; c.style.height=''; c.mh=c.offsetHeight; c.style.height=0}
			if(c.mh==c.offsetHeight){c.style.overflow='visible'}
			else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
		}else{p.className=p.className.replace(a,''); c.t=setInterval(function(){sl(c,-1)},t)}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')';
		c.style.height=h+(d*f)+'px'
	}
	return{dd:dd}
}();
</script>

     <div id="staticWrapper" style=" background-image: url(images/menu-bar-bg.png); background-repeat:repeat-x; height:147px; width:100%; z-index:-2; top:0px; position:absolute;">
    <div id="logo" style="width:953px; margin-left:auto; margin-right:auto; z-index:-1;"> <img src="images/static-logo-new.png" border="0" alt="" title="Syndicated Solar" style="margin-left:35px; margin-top:3px"/>
    </div>
     	<div id="staticMenu" style="width:953px; margin-left:auto; margin-right:auto; z-index:-1;">
        <ul class="menu" id="menu">
	<li><a href="#" class="homeLink">HOME PAGE</a>
		<ul>
			<li><a href="#">Navigation Item 1</a></li>
			<li>
				<a href="#" class="sub">Navigation Item 2</a>
				<ul>
					<li class="topline"><a href="#">Navigation Item 1 sub</a></li>
					<li><a href="#">Navigation Item 2 sub</a></li>
					<li><a href="#">Navigation Item 3 sub</a></li>
					<li><a href="#">Navigation Item 4 sub</a></li>
					<li><a href="#">Navigation Item 5 sub</a></li>
				</ul>
			</li>
			<li>
				<a href="#" class="sub">Navigation Item 3</a>
				<ul>
					<li class="topline"><a href="#">Navigation Item 1</a></li>
					<li><a href="#">Navigation Item 2</a></li>
					<li>
						<a href="#" class="sub">Navigation Item 3</a>
						<ul>
							<li class="topline"><a href="#">Navigation Item 1</a></li>
							<li><a href="#">Navigation Item 2</a></li>
							<li><a href="#">Navigation Item 3</a></li>
							<li><a href="#">Navigation Item 4</a></li>
							<li><a href="#">Navigation Item 5</a></li>
							<li><a href="#">Navigation Item 6</a></li>
						</ul>
					</li>
					<li><a href="#">Navigation Item 4</a></li>
				</ul>
			</li>
			<li><a href="#">Navigation Item 4</a></li>
			<li><a href="#">Navigation Item 5</a></li>
		</ul>
	</li>
       </ul>
  </div>
</div>

<style type="text/css">
	
ul.menu {list-style:none; margin:0; float:left; padding-left:10px; padding-top:14px;}
ul.menu * {margin:0; padding:0}
ul.menu a {display:block; color:#FFF; text-decoration:none}
ul.menu li {position:relative; float:left; margin-right:2px;}
ul.menu ul {position:absolute; top:26px; left:0; background:#d1d1d1; display:none; opacity:0; list-style:none; margin-top:9px; }
ul.menu ul li {position:relative; border:1px solid #828282;; border-top:none; width:148px; margin:0}
ul.menu ul li a {display:block; padding:3px 7px 5px; background-color:#202020; color:#bababa;}
ul.menu ul li a:hover {background-color:#CE470C; color:#222; }
ul.menu ul ul {left:148px; top:-1px}
ul.menu .menulink {border:0px; padding:5px 7px 7px; font-weight:bold; }
ul.menu .menulink:hover, ul.menu .menuhover {}
ul.menu .sub {background:#202020 url(images/arrow.gif) 136px 8px no-repeat; }
ul.menu .topline {border-top:1px solid #828282; }

 ul.menu .homeLink:hover {background:url(../../images/home.png) no-repeat; color:white; width:102px;}
 ul.menu .homeLink {background:none; width:102px; height:24px; text-align:center; padding-top:5px;}
 
 ul.menu .productsLink:hover {background:url(../../images/products.png) no-repeat; color:white; width:169px;}
 ul.menu .productsLink {background:none; width:169px; height:24px; text-align:center; padding-top:5px;}
 
 ul.menu .customersLink:hover {background:url(../../images/customers.png) no-repeat; color:white; width:131px;}
 ul.menu .customersLink {background:none; width:131px; height:24px; text-align:center; padding-top:5px;}
</style>

Open in new window


Thanks in advance!
0
Comment
Question by:Cybervanes
  • 7
  • 6
  • 4
17 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
1. http://scriptplayground.com/tutorials/as/Call-Javascript-in-Flash/

2.
var js = "document.links[3].onmouseover()"; // mouse over the 4th link on the page
 runJS(js);
 


0
 
LVL 2

Author Comment

by:Cybervanes
Comment Utility
so theoretically i should be able to call `document.links[3].onmouseover();` from an anchor tag as well right?

<a href="javascript:document.links[2].onmouseover();">click here</a>

this dose not work. error: ` document.links[2].onmouseover is not a function.`

any other ideas?
 
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
better


<a href="#" onClick="document.links[2].onmouseover(); return false">click here</a>

assuming document.links[2] HAS a mouseover
0
 
LVL 2

Author Comment

by:Cybervanes
Comment Utility
No that didn't work either...

the above script seems to be attaching onmouseover functions to the `UL` tags. but i cant figure out how to call those function for the individual tags.

if you would like i cloud email you a link to our testing server.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility

<a href="#" onClick="document.getElementsByTagName('ul')[2].onmouseover(); return false">click here</a>

my email is in my profile
0
 
LVL 2

Author Comment

by:Cybervanes
Comment Utility
that didn't work either...

emailed link.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Ah, ok I will look
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Sorry. Not a clue.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:stilliard
Comment Utility
have you tried the actionscript class ExternalInterface ? you can use it to call javascript from the page its called from. O think this could be used for the purpose you need.
more info and a similar issue here:
http://bytes.com/topic/flash/answers/694359-how-do-i-access-flash-function-using-javascript
0
 
LVL 2

Author Comment

by:Cybervanes
Comment Utility
Yes I'm aware of how to call JavaScript functions from flash. getURL("JavaScript:frunctionName()");

my question is based on the above script... what function would i call to activate the on-rollover functionality of each menu item.

thanks,
0
 
LVL 6

Expert Comment

by:stilliard
Comment Utility
well if you have jQuery on the page you could run $('.menulink:first').mouseover();
working on a raw js version atm
0
 
LVL 6

Expert Comment

by:stilliard
Comment Utility
this javascript should work.

document.getElementById('menu').getElementsByTagName('ul')[0].parentNode.onmouseover();
0
 
LVL 6

Accepted Solution

by:
stilliard earned 500 total points
Comment Utility
little explanation incase needed:

document.getElementById('menu').getElementsByTagName('ul')[0].parentNode.onmouseover();

get the menu element, then get all its child ul elements, select the first (index 0) and then get its parent element, then call the mouseover event on it.
So "document.getElementById('menu').getElementsByTagName('ul')" selects all the sub menus, then all thats needed was to select the one you want, in my case i referenced the first, get the parent node and trigger the event.
0
 
LVL 2

Author Comment

by:Cybervanes
Comment Utility
Very Nice stilliard... You need a promotion! testing and working good!
0
 
LVL 2

Author Closing Comment

by:Cybervanes
Comment Utility
great job! Thank You!
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
So I was really close
0
 
LVL 2

Author Comment

by:Cybervanes
Comment Utility
thank you both for your help!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

728 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