Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 阅读家长';s网址_Javascript_Jquery_Html_Iframe - Fatal编程技术网

Javascript 阅读家长';s网址

Javascript 阅读家长';s网址,javascript,jquery,html,iframe,Javascript,Jquery,Html,Iframe,我有一个iFrame,它运行一些Javascript,我希望iFrame根据加载到的页面的不同而表现不同。我发现这段代码工作得非常出色,但它显示的是iFrame的url,而不是父对象 var Page1 = "page1.html"; var Page2 = "page2.html"; var thisUrl = decodeURI(window.location); var urlChunks = thisUrl.split("/"); for (var chunk in urlCh

我有一个iFrame,它运行一些Javascript,我希望iFrame根据加载到的页面的不同而表现不同。我发现这段代码工作得非常出色,但它显示的是iFrame的url,而不是父对象

 var Page1 = "page1.html";
 var Page2 = "page2.html";
 var thisUrl = decodeURI(window.location);
 var urlChunks = thisUrl.split("/");

for (var chunk in urlChunks) {
alert('chunk: ' + chunk);
alert('urlChunks[chunk]: ' + urlChunks[chunk]);

if (urlChunks[chunk] == Page1) {
alert('inside index.html');

}

else if (urlChunks[chunk] == Page2) {

}

else
{

}

}
window.parent.location
我能换什么

decodeURI(window.location);
以便从父级读取

 var Page1 = "page1.html";
 var Page2 = "page2.html";
 var thisUrl = decodeURI(window.location);
 var urlChunks = thisUrl.split("/");

for (var chunk in urlChunks) {
alert('chunk: ' + chunk);
alert('urlChunks[chunk]: ' + urlChunks[chunk]);

if (urlChunks[chunk] == Page1) {
alert('inside index.html');

}

else if (urlChunks[chunk] == Page2) {

}

else
{

}

}
window.parent.location

请记住,JavaScript具有相同的来源限制,因此当父文档是不同的来源(例如域)时,您很可能会遇到访问被拒绝异常。

代码似乎没有运行=/。我的父母在Website/profile/Page1.html中,但我的框架在Website/frames/child.html中,这可能导致访问被拒绝异常否,在这种情况下不应触发任何异常。我的信息太少,无法告诉您为什么我的代码不适合您。应该这样。parent属性是获取框架文档父级的正确方法。此外,它是window.parent.location.url,否则您可能只是返回location对象。@Matt:不太可能。url不是window.location的属性。文档具有URL属性。如果您需要URL作为字符串,请使用parent.location.href,但如果您在location对象上使用decodeURI,它将转换为字符串。@Rafael我的错误。window.parent.location.hrefi如果您想要父url,这里已经回答了:这似乎不适用于me=S,它返回未定义的url,而不是父url。我的文件结构可能会影响此操作。@eddy147抱歉,没有标记您的名字