我一直在为一个主页设计的js文件上工作。 我想通过导航菜单栏从此页导航到其他页面。 目标页面共享相同的模板(一个html代码),因此要转到特定的页面,我需要加载一个特定的内容,它保存在一个xml文件中,然后将其内容传递到目标页面。从html页面的url获取文档对象
function loadFileToElement(filename, elementId)
{
var xhr = new XMLHttpRequest();
try
{
xhr.open("GET", filename, false);
xhr.send(null);
}
catch (e) {
window.alert("Unable to load the requested file.");
}
// Until this point I can load the specific content
// How can I get from the url of the target page
// a js document object, so that I can call getElementById(Id)
// to pass the specific content.
// For instance: Im currently opnening X1:= www.main.com
// und I would like to switch to X2 := www.targetpage.com
// target page which contains html the templat.
// The problem **document** represents currently X1
// but i would like to set it to X2 so that I can pass
// the content of xhr.responseText to it
var component = **document**.getElementById(elementId);
component.innerHTML = xhr.responseText;
}
Thanks
我你首先坦克的快速答复。但内部函数'xhr.onload = function(){}'不适用于我。我试图在这个函数中显示一个警告消息。但它不起作用 – user2315181
@ user2315181,我已更新回答 – Satpal
对不起,也许我不清楚,我的意思是以下内容:例如:我目前正在运行X1:= www.main.com und我想切换到X2:= www.targetpage.com包含html模板的目标页面。问题***文档***代表当前X1,但我想将其设置为X2,以便我可以将xhr.responseText的内容传递给它 – user2315181