是否有WordPress插件可以实现深层链接到嵌入式iframe?例如,我希望能够将帖子的URL发送到帖子,该帖子包含将传递给iframe的额外信息。
一个例子是播放视频的iframe。这种情况下的额外信息可能是开始播放视频的时间偏移。
额外信息可以作为查询参数,片段或其他方式传递。
答案 0 :(得分:1)
可能不是通过WordPress插件,除非您正在寻找开发自定义插件。
最好尽可能避免使用iframe these reasons。
也就是说,使用window.postMessage
方法和works in most browsers,包括IE8及更高版本,解决方案非常简单。
注意:
JSON.serialize()
window.location
个对象。如果您尝试传递该对象,则必须逐个复制属性。el.contentWindow.postMessage()
,而不支持el.postMessage()
。外页
window.onload = function()
{
var child = document.getElementById('deep_link_frame');
var msg = {
"location" : {
"hash" : window.location.hash,
"host" : window.location.host,
"hostname" : window.location.hostname,
"href" : window.location.href,
"origin" : window.location.origin,
"pathname" : window.location.pathname,
"port" : window.location.port,
"protocol" : window.location.protocol,
"search" : window.location.search
}
};
child.contentWindow.postMessage(JSON.stringify(msg), '*');
};
内页
function bindEvent(el, eventName, eventHandler)
{
if (el.addEventListener)
{
el.addEventListener(eventName, eventHandler);
}
else
{
el.attachEvent('on' + eventName, eventHandler);
}
}
bindEvent(window, 'message', function(e)
{
if (e.origin === "http://your-domain.com")
{
var message = JSON.parse(e.data);
alert(message.location.href);
}
});