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

accessing an element from another window 1

Status
Not open for further replies.

uncLez

Programmer
Jun 18, 2002
38
NL
Hi all,
I am having a problem here...

from a window I open a new one and from the new one I want to run a script that changes an element value in the first one.

I tested this with a hidden form element, which works fine. But now I need to perform this on an input element in a div somewhere else in that document. Abd the thing is, I can't seem to get a hold of that element.

When I access it in the opener window it works...
test line:
Code:
alert('URL = ' + document.getElementById("idLinkURL").getAttribute("value"));

When I try to access it from the newly opened window, like this:
Code:
alert('URL now is :' + window.opener.document.getElementById("idLinkURL").getAttribute("value"));

I get a "window.opener.document.getelementbyid(...) is empty or no object" error message

Can someone help me out on this?

Thanx in advance,
Anton.
 
window.opener?

Have you tryed with only opener.getElementById...?

I think this will workout. I don't know if it works on Netscape, but it works on IE. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
if idLinkURL is the ID of an <A> tag then try saying:

alert(&quot;URL now is :&quot;+window.opener.idLinkURL.href);
 
sjravee: actually it is an <input type=&quot;text&quot;> tag. Weird thing is that I am able to change an <input type=hidden> tag no problem. But the tags inside a <div> I cannot seem to access. While it does work when accessed from the page itself.

Anikin: I will try, but I don't hink it will make much difference. And I CAN access a form element this way... Just not inside the <DIV> thingy :(
 
Well, more or less :)

There is a form (the one that I can access) in which a huge file gets attached. this consists of shitloads of layers and so on. Like I said I can access the elements from within that page no problem. Just like this: document.getelementbyid(&quot;elementid&quot;)

In my perception I should be able to access the element from another window just about the same way... But somehow for this input tag that does not work, while for input tags outside of div's it does work...

heh, hope this makes sense...
 
ok, if it works on the containing page then call a function in the containing page from the popup page also passing in the new value for the text field

from the popup window just say

window.opener.SetTextValue(newvalue)


in your containing page have a script function called SetTextValue

function SetTextValue(thenewval)
{
document.getElementById(&quot;idLinkURL&quot;).value = thenewval
}
 
Hmmz, this is getting weird...

I cannot even run a function like that??? the function:
Code:
function SetInternal(internelink)
	{
	document.getElementById(&quot;idLinkURL&quot;).value = internelink;
	}

The function calling the function from the pop-up window...
Code:
function ExportLink(IDwaarde)
	{
	var Stringel;
	Stringel = 'Pagina ' + IDwaarde;
	alert('Stringeltje = ' + Stringel);
	window.opener.SetInternal(Stringel);
	window.close('Preview');
	}

And I get an &quot;the property or method is not supperted by this object&quot; error message...

I'm going insane :]
 
when you opened the window did you set a reference to it ie:

var newwin =window.open(...
 
Yeah...

Gets done in a function as well.

Like this:
Code:
function InsertInternalLink()
	{
	var newwin = window.open('ContEd_Admin_InterneLink.asp','Intern','titlebar,scrollbars,width=340,height=480');
	}
 
try var ing it outside the function, ie make it global and setting the opener

ie:

var newwin = '';

function InsertInternalLink()
{
if (newwin.location && !newwin.closed)
{ newwin.location.href=&quot;ContEd_Admin_InterneLink.asp&quot;;
}
else
{
newwin=window.open('ContEd_Admin_InterneLink.asp','name','height=200,width=150');
if (!newwin.opener) newwin.opener = self;
}
if (window.focus) {newwin.focus()}
}
 
Heh... this is getting very frustrating =(

It still does not work...

Thanx so much for all your help though, dude!

I'll just show you all the code that is involved, just to make sure...

The elements involved:
Code:
<td>
 
<select id=&quot;idLinkType&quot; NAME=&quot;idLinkType&quot; onchange=&quot;CheckInternal();&quot;>
<option value=&quot;&quot; selected></option>
<option value=&quot;intern&quot;>intern:</option>
<option value=&quot;[URL unfurl="true"]http://&quot;[/URL] selected>[URL unfurl="true"]http://</option>[/URL]
<option value=&quot;[URL unfurl="true"]https://&quot;>https://</option>[/URL]
<option value=&quot;mailto:&quot;>mailto:</option>
<option value=&quot;ftp://&quot;>ftp://</option>
<option value=&quot;news:&quot;>news:</option>
</select>
<input type=text size=30 id=&quot;idLinkURL&quot; value=&quot;&quot; style=&quot;height: 19px;font:8pt verdana,arial,sans-serif&quot; NAME=&quot;idLinkURL&quot;>
</td>

The functions that process the onChange:
Code:
<script language=javascript>
var newwin = '';

function InsertInternalLink()
	{
	if (newwin.location && !newwin.closed)
		{
		newwin.location.href = 'ContEd_Admin_InterneLink.asp';
		}
	else
		{
		newwin = window.open('ContEd_Admin_InterneLink.asp','Intern','titlebar,scrollbars,width=340,height=480');
		if (!newwin.opener)
			{
			newwin.opener = self;
			}
		if (window.focus)
			{
			newwin.focus()
			}
		}
	}

function CheckInternal()
	{
	if (document.getElementById(&quot;idLinkType&quot;).selectedIndex == 1)
		{
		InsertInternalLink();
		}
	}
	
function SetInternal(internelink)
	{
	document.getElementById(&quot;idLinkURL&quot;).value = internelink;
	}
</script>

The function that tries to put the value back into the first page:
Code:
<script language=javascript>
function ExportLink(IDwaarde)
	{
	var Stringel;
	Stringel = 'Pagina ' + IDwaarde;
	window.opener.SetInternal(Stringel);
	window.close();
	}
</script>
And stil getting the object does not support the property/method error on the window.opener.SetInternal line...

Anything I missed/looked over/mistyped/misinterpreted? I am running out of options :)

And thanx again for your help so far, m8!
 
Bad news, it works! in IE55, this is wierd!

Ok the following code works fine in IE55, all i did was modify the code slighly to add a form tag in there and a div tag


[MAIN PAGE]:

<html>
<head>
<script language=javascript>
var newwin = '';

function InsertInternalLink()
{
if (newwin.location && !newwin.closed)
{
newwin.location.href = 'popup.htm';
}
else
{
newwin = window.open('popup.htm','Intern','titlebar,scrollbars,width=340,height=480');
if (!newwin.opener)
{
newwin.opener = self;
}
if (window.focus)
{
newwin.focus()
}
}
}

function CheckInternal()
{
if (document.getElementById(&quot;idLinkType&quot;).selectedIndex == 1)
{
InsertInternalLink();
}
}

function SetInternal(internelink)
{
document.getElementById(&quot;idLinkURL&quot;).value = internelink;
}
</script>
</head>
<body>
<form name=Test>
other inputs go here i take it<br>
<div id=Holding>
<table><tr><td>
<select id=&quot;idLinkType&quot; NAME=&quot;idLinkType&quot; onchange=&quot;CheckInternal()&quot;>
<option value=&quot;&quot; selected></option>
<option value=&quot;intern&quot;>intern:</option>
<option value=&quot; selected><option value=&quot;<option value=&quot;mailto:&quot;>mailto:</option>
<option value=&quot;ftp://&quot;>ftp://</option>
<option value=&quot;news:&quot;>news:</option>
</select>
<input type=text size=30 id=&quot;idLinkURL&quot; value=&quot;&quot; style=&quot;height:19px; font:8pt verdana,arial,sans-serif&quot; NAME=&quot;idLinkURL&quot;>
</td></tr></table>
</div>
</form>
</body>
</html>

[POPUP.HTM]:

<html>
<head>
<script language=javascript>
function ExportLink(IDwaarde)
{
var Stringel;
Stringel = 'Pagina ' + IDwaarde;
window.opener.SetInternal(Stringel);
window.close();
}
</script>
</head>
<body>
ADD:<input type=text id=AddText><a href=&quot;#&quot; onclick=&quot;ExportLink(AddText.value);return false&quot;>add</a>
</body></html>



Does this example work on your browser?
 
Like a breeze...

omg! =]

Could it be that the way in which all the content gets included somehow screws this up?

Like this
<html XMLNS:ACE>

=> Rest of the Form...

=> The huge file gets attached here...

<ACE:AdvContentEditor id=&quot;idContent&quot; content=&quot;&quot; onSave=&quot;Save();&quot; onImgLookupOpen=&quot;OpenImgLookup()&quot; />

Coz everything SHOULD work now... I mean, it does, except when my I try to access elements inside this attached stuff...
 
Probably best start off with the basic script we just wrote, then start adding your extra scripts and includes to the page one at a time, each time testing to make sure everything works. This should point you in the general direction of the troublesome code
 
I'll try some more options.
Thanx for all yer help, dude.

You da man... :]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top