views:

1331

answers:

3

I have an HTML page which contains an Object tag to host an embedded HTML page.

<object style="border: none;" standby="loading" id="contentarea" 
 width="100%" height="53%" type="text/html" data="test1.html"></object>

However, I need to be to change the HTML page within the object tag. The current code seems to create a clone of the object and replaces the existing object with it, like so:

function changeObjectUrl(newUrl)
{
    var oContentArea = document.getElementById("contentarea");
    var oClone = oContentArea.cloneNode(true); 
    oClone.data = newUrl; 

    var oPlaceHolder = document.getElementById("contentholder"); 
    oPlaceHolder.removeChild(oContentArea); 
    oPlaceHolder.appendChild(oClone); 
}

This seems a rather poor way of doing this. Does anyone know the 'correct' way of changing the embedded page?

Thanks!

EDIT: In response to answers below, here is the full source for the page I am now using. Using the setAttribute does not seem to change the content of the Object tag.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<script language="JavaScript">
function doPage()
{
    var objTag = document.getElementById("contentarea");
    if (objTag != null)
    {
     objTag.setAttribute('data', 'Test2.html');
     alert('Page should have been changed');
    }
}
</script>
</head>
<body>
<form name="Form1" method="POST">
<p><input type="button" value="Click to change page" onclick="doPage();" /></p>
<object style="visibility: visible; border: none;" standby="loading data" id="contentarea" title="loading" width="100%" height="53%" type="text/html" data="test1.html"></object>
</form>
</body>
</html>

The Test1.html and Test2.html pages are just simple HTML pages displaying the text 'Test1' and 'Test2' respectively.

+2  A: 

You can do it with setAttribute

document.getElementById("contentarea").setAttribute('data', 'newPage.html');

EDIT: It is also recommended that you use the window.onload to ensure that the DOM has loaded, otherwise you will not be able to access objects within it.

It could be something like this:

function changeData(newURL) {
    if(!document.getElementById("contentarea")) 
        return false;
    document.getElementById("contentarea").setAttribute('data', newURL);
}

window.onload = changeData;

You can read more about window.onload here

andi
I can't get this to work at the moment for some reason. The object still displays the old page. I will post the full HTML page I am using later if I don't have any Eureka moments.
Tim C
But is the selector working? If you alert(document.getElementById("contentarea")) what does it say? If it says Undefined, then the DOM is not loaded. You should use document.onload. I will add it in my answer because the characters here are not enough.
andi
A: 

Changing the data attribute should be easy. However, it may not work perfectly on all browsers.

If the content is always HTML why not use an iframe?

I am told by my project leader, The iframe element is not supported in HTML 4.1 Strict DTD and in XHTML 1.0 Strict DTD and so he doesn't want to use them, the big meanie.
Tim C
you never get anywhere on a project, following standards...
Magnus Smith
A: 

Here's how I finally achieved it. You can do

document.getElementById("contentarea").object.location.href = url;

or maybe

document.getElementById("contentarea").object.parentWindow.navigate(url);

The Object element also has a 'readyState' property which can be used to check whether the contained page is 'loading' or 'complete'.

Tim C