Jump to content

Optimize code?


lindm

Recommended Posts

Got a small piece of code for a tooltip popup window:

 

<a class="h" onmouseover="Tip(t17)" onmouseout="UnTip()">(?)</a>

 

This snip occurs many times on the page with the variable t1 to t100. Is there a way to make the code smaller (save space)? I have been thinking of a few options:

 

1. Only using one event handler. Perhaps some type of onchange event...

2. Using css to handle the tooltip. Is it possible to call a javascript within css (hover: script(); or something...)

 

Any help appreciated.

Link to comment
https://forums.phpfreaks.com/topic/119021-optimize-code/
Share on other sites

You can't use javascript in CSS, but you can dynamically set all the events when the page loads:

<html>
<head>
<script type="text/javascript">
function initPage()
{
var as = document.getElementsByTagName('a');
for (var i=0;i<as.length;i++)
{
	as[i].onmouseover = function()
	{
		//tip code
	}
	as[i].onmouseover = function()
	{
		//kill tip
	}
}
}
</script>
</head>
<body onload="initPage()">
<a>test</a><br>
<a>test2</a><br>
<a>test3</a><br>
<a>test4</a><br>
<a>test5</a><br>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/119021-optimize-code/#findComment-615922
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.