0

如果页面上存在元素,是否可以更改页面内容?

if (window.frames[0].document.getElementById('xl')) { //加载页面内容acta.html}

4

1 回答 1

1

Only if the content loaded in the frames is in the same domain of the page where the script is executed. See the Same-Origin Policy – SOP.

Otherwise you can't access to the frame's DOM.

If the page are in the same domain, then your code is perfectly valid – assume the page in the frames is loaded.

Update:

To change the content, then you can simple change location object of the frame:

if (window.frames[0].document.getElementById('xl')) {
    window.frames[0].location.href = "acta.html";
}

If you have a reference to the frame / iframe element instead of the window's (frame[0]), you can also use the src attribute.

You can also change the top window's location just doing:

window.location.href = "acta.html";

However that will change, of course, the URL of the page too. If you want to keep the URL as before, you should use another iframe that basically wraps your current main window and the hidden frame. So the URL displayed in the address bar will not change even if you change the content of the first frames to "acta.html".

Another approach could be use the XMLHttpRequest object to makes a http call and load your page's source, then replace the body of the current page with the one's retrieved by the XHR. However, that's ugly. This kind of redirection, keeping the same URL, should be done on server side, not on client side.

于 2013-04-06T11:09:14.970 回答