gpt4 book ai didi

javascript - 单击 Iframe 中的链接时使用 history pushState 更改父窗口的 URL

转载 作者:行者123 更新时间:2023-11-30 09:52:44 24 4
gpt4 key购买 nike

我的页面有一个页眉和侧边栏,右边的内容面板是一个 Iframe。

在加载到右侧内容面板的页面中,我试图让点击的链接将父窗口中的浏览器 URL 更新为加载到 Iframe 的新页面的 URL。

我不希望实际的父窗口重新加载 URL,而只是更新地址栏中的 URL。

类似于:

window.history.pushState('obj', 'newtitle', '/bookmarks/list/');

这可能来自 Iframe 吗?

最佳答案

通过使用 postMessage 从子 Iframe 窗口向父窗口发送新 URL 并在父窗口监听这个事件。

当父级收到子 iframe postMessage 事件时,它使用在该消息中传递的 URL 更新带有 pushSTate 的 URL。

子 iframe

<script>
// Detect if this page is loaded inside an Iframe window
function inIframe() {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}

// Detect if the CTRL key is pressed to be used when CTRL+Clicking a link
$(document).keydown(function(event){
if(event.which=="17")
cntrlIsPressed = true;
});
$(document).keyup(function(){
cntrlIsPressed = false;
});
var cntrlIsPressed = false;

// check if page is loaded inside an Iframe?
if(inIframe()){
// is the CTRL key pressed?
if(cntrlIsPressed){
// CTRL key is pressed, so link will open in a new tab/window so no need to append the URL of the link
}else{

// click even on links that are clicked without the CTRL key pressed
$('a').on('click', function() {

// is this link local on the same domain as this page is?
if( window.location.hostname === this.hostname ) {

// new URL with ?sidebar=no appended to the URL of local links that are clicked on inside of an iframe
var linkUrl = $(this).attr('href');
var noSidebarUrl = $(this).attr('href')+'?sidebar=no';

// send URL to parent window
parent.window.postMessage('message-for-parent=' +linkUrl , '*');

alert('load URL with no sidebar: '+noSidebarUrl+' and update URL in arent window to: '+linkUrl);

// load Iframe with clicked on URL content
//document.location.href = url;
//return false;
}
});
}
}
</script>

父窗口

<script>

// parent_on_message(e) will handle the reception of postMessages (a.k.a. cross-document messaging or XDM).
function parent_on_message(e) {
// You really should check origin for security reasons
// https://developer.mozilla.org/en-US/docs/DOM/window.postMessage#Security_concerns
//if (e.origin.search(/^http[s]?:\/\/.*\.localhost/) != -1
// && !($.browser.msie && $.browser.version <= 7)) {
var returned_pair = e.data.split('=');
if (returned_pair.length != 2){
return;
}
if (returned_pair[0] === 'message-for-parent') {
alert(returned_pair[1]);
window.history.pushState('obj', 'newtitle', returned_pair[1]);
}else{
console.log("Parent received invalid message");
}

//}
}

jQuery(document).ready(function($) {
// Setup XDM listener (except for IE < 8)
if (!($.browser.msie && $.browser.version <= 7)) {
// Connect the parent_on_message(e) handler function to the receive postMessage event
if (window.addEventListener){
window.addEventListener("message", parent_on_message, false);
}else{
window.attachEvent("onmessage", parent_on_message);
}
}
});
</script>

关于javascript - 单击 Iframe 中的链接时使用 history pushState 更改父窗口的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35518763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com