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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem in displaying an ad that uses javascript

Status
Not open for further replies.

Luv2Live

Programmer
Joined
Jun 12, 2006
Messages
1
Location
US
I have a <div> tag that exists on an asp page. I have written javascript code to move into that div an ad that I randomly pull from a database. The div tag looks like this:

Code:
<div id="pos-SKY"></div>

I have a javascript routine that returns all divs that have a 'pos' prefix:

Code:
var posArray2 = new Array(); 
var posdivs = document.getElementsByTagName("div"); 
var divName;

	 for (var i = 0; i < posdivs.length; i++) { 
	    if (posdivs[i].getAttribute('id') != null && posdivs[i].getAttribute('id').match('pos-')) { 
	      posArray2[posArray2.length] = posdivs[i].getAttribute('id'); 
	      divName =posdivs[i].getAttribute('id');
	      divName2 = divName.replace("pos-", "");
	      idName = "ad-" + divName2;
	      idContent = "content-" + divName2;
	      idClass = "class-" + divName2;
		  document.write('<div id="' + idContent + '"><div id="'+ idName + '" style="position:absolute"><script type="text/javascript">POSServing("' + divName2 + '");setLyr("' + divName + '","' + idName + '","' + idContent + '");</script></div></div> ');	                                                                                                                              
	    } 
	  }
The POSServing is the routine that uses the recordset to return the appropriate ad from the database.

I do a document.write('<%=link%>') to return the ad. This works most of the time unless I have a flash ad that consists of javascript. Then, the ad doesn't layer on the page correctly. Here is the setLyr code.

Code:
function setLyr(obj,lyr,container)
{

	var o = xGetElementById(obj);
	var newX = findPosX(o);
	var newY = findPosY(o);
	var leftcontainer1= findPosX(xGetElementById(container));
	var x = xGetElementById(lyr);
	x.style.top = newY + 'px';
	newX= newX - leftcontainer1;
	x.style.margin = '0 0' + newX + 'px 0';
	}
function findPosX(obj)
{
	
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
function xGetElementById(e) {
  if(typeof(e)!="string") return e;

  if(document.getElementById){ 
	e=document.getElementById(e);

		}
  else if(document.all)  e=document.all[e]; 
  else if(document.layers) e=xLayer(e);
  else e=null;
  return e;
}
function xLayer(id,root) { // only for nn4
	
  var i,layer,found=null;
  if (!root) root=window;
  for(i=0; i<root.document.layers.length; i++) {
    layer=root.document.layers[i];
    if(layer.id==id) return layer;
    if(layer.document.layers.length) found=xLayer(id,layer);
    if(found) return found;
  }
  return null;
}
function xShow(e) {
  if(!(e=xGetElementById(e))) return;
  if(e.style && xDef(e.style.visibility)) e.style.visibility='inherit';
  else if(xDef(e.visibility)) e.visibility='show';
 
}
function xHide(e) {
  if(!(e=xGetElementById(e))) return;
  if(e.style && xDef(e.style.visibility)) e.style.visibility='hidden';
  else if(xDef(e.visibility)) e.visibility='hide';
}

In FireFox, the ad will display as if I didn't have Flash installed because of the <NOSCRIPT> tags. In IE, I just can't position the ad correctly. The ad will display after the body of the page instead of within it.

Has anyone experienced anything similiar to this? I am pretty new to javascript and I am at a lost. Thanks!
 
If you want the div to be in the page flow, why bother with any of the positiong code? Simply ditch it, and put the div where you want it in your markup.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top