Jump to content

Script to Read an online page's source and return a value from a certain ID


quantumdecipher

Recommended Posts

I have a small project, which needs to read a status from an online page.

 

Basically, I need a script to search for a certain tag ID from say www.foo.com/page1.html

 

What I intend to do is get the ID of a certain tag and then save the inner HTML of it into a variable.

 

Any ideas? I tried loading the page in an iFrame and trying JS to get an ID but fails.

 

This is what i tried.

 

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

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

<script type="text/javascript">

function getContentFromIframe(iFrameName) {

  var myIFrame = document.getElementById(iFrameName);

  var content = myIFrame.contentWindow.document.body.innerHTML;

  var status = content.getElementById('AREA:222_4');

  alert(status);

}

</script>

</head>

 

<body>

<iframe id="emap" src="http://emaplin3.foo.com/page1.html">

</iframe>

<input type="button" value="Get Status" onclick="Javascript: getContentFromIframe('emap')" />

</body>

</html>

 

Now I'm lost.

getElementById is a method of the document object. Should be:

 

var content = myIFrame.contentWindow.document;

 

Edit: although you might want to consider changing the name of the variable, as it's a little misleading now.

Thanks for the Reply, but it still doesn't work.

 

What I am trying to do is, say i got a page, index.html, then i got another page content.html.

I have an iframe in index.html which contains content.html. i also have a button in index.html which when clicked needs to return an id from a tag inside content.html.

 

I am trying to make a script which gets the innerHTML of a tag with the id provided by the user inside content.html.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.