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

Finding the parent object of a script

Status
Not open for further replies.

mgh730

Programmer
Jun 12, 2002
23
US
I have the following code:

Code:
<script>
function toolbar() {
   newDiv = document.createElement("div");
   ???.appendChild(newDiv);
}
</script>

<body>
<span class="toolbar">
     <script>toolbar( ... )</script>
</span>
</body>

In the function toolbar, I create a new element called newDiv. I want to insert this element inside the <span> tag using appendChild (or any method). How can I do this?

Perhaps there is a way of getting the parentNode of the <script> tag (the tag inside the span)? I do not want to name or id anything (so that I can have multiple instances of the code without changing anything).
 

If you change your function to read:

Code:
function toolbar() {
   document.writeln('<div>xyz</div>');
}

That would be much the same.

Hope this helps,

Dan

 
That would normally work, but in this case, the document may not call the function on load. My example above is extremely simplified... but what I am writing does require the code to execute both onload and afterwards. The document is very dynamic...

What I need is a simple method of determining the parent node of script tags in the document body. This needs to be found when code is executed inside the script tags, without using predetermined string constants (by which I mean that I cannot use getElementById and an id string).

Thanks, though, for the response.
 

I don't believe you can get the position in a document that the script tag is placed in programatically.

Sorry!

Dan
 
I'm not sure if this is what you are after. Run it and see. You can find all SCRIPT tags within a document, and then simply exclude any which you don't want...
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title>Find All Script Tags...</title>
<script>
function findScripts() {
	var allScripts = document.getElementsByTagName("script");
	for(var x=0;x<allScripts.length;x++) {
		alert("Script found, ParentNode is:" + allScripts[x].parentNode.tagName + " and it's class is:" + allScripts[x].parentNode.className);
	}
}
</script>
</head>
<body onload="findScripts()">
<span class="toolbar">
     <script>//toolbar( ... )
	 </script>
</span>
<span class="toolbar1">
     <script>//toolbar( ... )
	 </script>
</span>
<span class="toolbar2">
     <script>//toolbar( ... )
	 </script>
</span>
</body>
</html>

Hope this helps.

Pete.


Web Developer &amp; Aptrix / Lotus Workplace Web Content Management (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
NOTE: The getElementsByTagName function returns an array of objects [thumbsup2]

Pete.


Web Developer &amp; Aptrix / Lotus Workplace Web Content Management (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
I had thought of doing something like this, but I haven't figured out how to identify which of the resulting spans contains the script that was just run. I see that you have given each span a different class as a way to identify them, but if they all have the same class and no id, I can't think of a way to make it work.

Anyway, I have given up implementing something like this in my current project, but I still am wondering whether what I am trying to do is possible... if it is, it could be useful... or it could be useless. :)

Thanks, though, for taking the time to write that code.
 
I guess I'm still not exactly sure what you are trying to achieve. Another method could be to write out dynamic spans wherever you want to use them but use a global index variable to number them... such as:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title>Find All Dynamic Span Tags...</title>
<script>
i=0;
function OutputSpan(num) {
	document.write("<span id='span"+num+"'></span>");
}
function findSpans() {
	for(x=0;x<i;x++){
		var obj=document.getElementById('span'+x);
		obj.innerHTML="Span: "+x;
	}
}
</script>
</head>
<body onload="findSpans()">
<script>OutputSpan(i++);</script>
<div>Text after span</div>
<script>OutputSpan(i++);</script>
<div>Text after span</div>
<script>OutputSpan(i++);</script>
<div>Text after span</div>
</body>
</html>

What is the application for your webpage?

Pete.


Web Developer &amp; Aptrix / Lotus Workplace Web Content Management (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top