Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

frikfrak

macrumors regular
Original poster
Sep 17, 2007
101
0
I'm a beginner, so I'm sure I'm doing something stupid. In loadIt(), I open a page in a second window, and get a reference to the window object, x. In the first line in loadFromDs(), I get an element from the second window by ID, and set it to y.

This works fine when run locally in safari, no error. Put it on my server and run it and the first statement of loadFromDs() generates this error: TypeError: Result of expression 'x.document' [undefined] is not an object.

What am I doing wrong? Thanks. Here's my code:

Code:
var x;
var y;

function loadIt()
{
	var g = "http://www.autofind.com/group/listing/216-2389/used/?f_make=All&f_model=All&f_trim=All&f_year=All&f_body=All&f_stockvin="
	x=window.open(g + document.getElementById("stock").value,'vehicle','width=200,height=100')		
}

function loadFromDs()
{
var y=x.document.getElementById("trow0");
var p = y.innerHTML;
document.getElementById("output2").value = p;
//do more stuff
}
 
How/when does loadIt get called? If it's not running before the other function, then x is not defined.
 
loadIt is called first with a button. When I can see the page has fully loaded, I call loadFromDs also with a button. Works without error when run locally. Errors every time when run from the server.
 
I tried to test locally, but the created window doesn't have permission to talk to my test page so that won't work. One thing to try, I noticed the window creation line has extra white space at the end. And even though it's technically not required, you should end each code line with a semicolon. Another note, I noticed my pop-up blocker blocked the second window from opening. I had to disable it for that to work.
 
Thanks for trying to help. Fixed the missing semicolon, but the result is still the same.

Anyone else have any ideas? I can send you the entire script if ur interested.

tnx
 
Code:
var x;
var y;

function loadIt()
{
	var g = "http://www.autofind.com/group/listing/216-2389/used/?f_make=All&f_model=All&f_trim=All&f_year=All&f_body=All&f_stockvin="
	x=window.open(g + document.getElementById("stock").value,'vehicle','width=200,height=100')		
}

function loadFromDs()
{
var y=x.document.getElementById("trow0");
var p = y.innerHTML;
document.getElementById("output2").value = p;
//do more stuff
}
Try changing the first line of loadFromDs (comment the old out for backup), to y = x.document; and then comment out the other two lines below it. Then run it on your server, if there is an error still then I'd guess x is null.

You could also try showing x in an alert box.
 
Thanks Mythic. X shows the DOM object. Y shows "undefined."

Here's a simplified example. Run this locally, and then on a server. Click Get Window, and after the window loads, click Info. For me, locally I get "theWindow" in the alert. On the server, I get "undefined."

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

	<title>untitled</title>
	
	<script type="text/javascript">
	
	var myWindow;//global
	
	function getWindow()
	{
		myWindow = window.open("http://www.google.com", 'theWindow', 'width=200,height=100');
	}
	
	function info()
	{
		alert(myWindow.name);
	}	
	
	</script>
	
</head>

<body>
	
	<input type="button" id="button1" onclick="getWindow()" value="Get Window"/>
	<input type="button" id="button2" onclick="info()" value="info"/>

</body>
</html>
 
Try changing this one line of code:
Code:
myWindow = window.open("http://www.google.com", 'theWindow', 'width=200,height=100');
to
Code:
myWindow = window.open("http://www.google.com", "theWindow", "width=200, height=100");

If that does nothing, then try the below:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

	<title>untitled</title>
	
	<script type="text/javascript">
	
	var myWindow;//global
	
	function getWindow()
	{
		myWindow = window.open("http://www.google.com", 'theWindow', 'width=200,height=100');
                alert(myWindow);
                alert(myWindow.name);
	}
	
	function info()
	{
                alert(myWindow);
		alert(myWindow.name);
	}	
	
	</script>
	
</head>

<body>
	
	<input type="button" id="button1" onclick="getWindow()" value="Get Window"/>
	<input type="button" id="button2" onclick="info()" value="info"/>

</body>
</html>

I added two lines below the only one in the getWindow() function, and added one to the info() function.

There are four alerts, tell me what they say on your server and if the results differ locally. Click getWindow() first and then info(), and then also run your script again but click info() and don't click getWindow() and tell me what it says.

Thanks
 
Changing the window.open statement made no difference; worked the same either way.

With all the alerts, locally, it all works as expected:

1st button
1st alert - Object DOMWindow
2nd alert - theWindow

2nd button
1st alert - Object DOMWindow
2nd alert - theWindow

But when the same code is loaded from the server, I get:

1st button
1st alert - Object DOMWindow
2nd alert - undefined

2nd button
1st alert - Object DOMWindow
2nd alert - defined

Thanks, any other ideas/thoughts?
 
Are both pages on the same domain name? Working locally it wouldn't matter, but on the web there can be permission issues like the one I described earlier. I made an assumption both pages were on the same domain, but thought I should ask directly just in case.
 
You nailed it Angelwatt! Thank you. If my html file is on my own server, and I open a page on my domain in the new window, everything works fine. Change the window's target to another domain and my window object is undefined.

thanks guys. Back to the drawing board. How can I get at the html of an external page?
 
How can I get at the html of an external page?

Not really, but depends on some specifics. You would probably need to use AJAX and possibly some server-side code to help do some grunt work. From PHP you could make use of the curl program that can fetch pages, but will take some effort to use if you're not familiar with PHP. What you're wanting is hard because it can be a security issue. If pages are able to talk too easily malicious pieces can find their way in and cause users big headaches.
 
Made some progress using PHP, but have a ways to go. What I'm trying to do is parse pages that contain product descriptions/pricing, assemble and analyze and compare the data. We currently do it manually now, going to websites and entering the data into spreadsheets. Thanks for taking the time to help.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.