Opening a page in a new window and using JS to see inside

Hi folks,

Here’s a puzzler for you :slight_smile:

I want to create a new window with a page loaded inside and use javascript in the calling page to examine the contents of the new page (after waiting a couple of seconds) and based on what the new page holds branch off in different directions. Its almost a bit like page scraping but client side on my PC through the browser.

How would I accomplish this, I know the basics of javascript (very basics) and thought something along the lines of this might work:


<html> 
<script language="javascript"> 


function openwindow() 
{ 
winobj = window.open("http://www.google.com","tester1","height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
} 


function chkstuff() 
{ 
newstuff = winobj.document.getElementById("body").value;
alert(newstuff);
}

</script> 


<body> 
<form name=frm> 
<input id=text1 type=text> 
<input type=button onclick="javascript:openwindow()" value="Open window.."> 
<input type=button onclick="javascript:chkstuff()" value="Chkstuff"> 
</form> 
</body> 
</html>

But it doesnt and Ive not really done much coding in the last while (before I was doing more PHP stuff).

Could someone lead me in the right direction on this one please.

many thanks,

Rik

PS: That code above was just me trying to access the page thats loaded up and ALERT it up on screen, just in case anyone thought I was completely crazy :slight_smile:

javascript alone cannot read or write to a page from a different domain than the one with the script.

Are you trying to access the DOM of an external webpage? Javascript can’t do this alone, but PHP can help. You can use this function in PHP to put a webpage’s DOM in your page.


<div id="anothersite">
<?php
echo file_get_contents("http://www.google.com/");
?>
</div>

That will throw an external webpage’s markup into your webpage. With CSS, you can even hide the div from sight like this


#anothersite {
display: none;
}

From there, you can access the DOM just as you would access anything else in your webpage. Good luck!

Ah well there lies my problem LOL

Knew it wouldnt be that easy :slight_smile:

Thanks for the help guys, mucho appreciated.

Rik

Only if the opened page is on the same domain as the opener :slight_smile:

I thought that if a script opened a new child window it could then read the contents of that child window though?