admin管理员组

文章数量:1406924

I want to run a pushState and it works fine but when I load the page through an iframe element it refused to work. This is how my code looks like

$('a[href]').click(function){
    newHref = $(this).attr('href');
    history.pushState('', '', newHref);
})

But it's not working, Please anyone with an idea of how to achieve this?

I want to run a pushState and it works fine but when I load the page through an iframe element it refused to work. This is how my code looks like

$('a[href]').click(function){
    newHref = $(this).attr('href');
    history.pushState('', '', newHref);
})

But it's not working, Please anyone with an idea of how to achieve this?

Share Improve this question asked Nov 8, 2016 at 15:11 doggie brezydoggie brezy 2973 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Using history.pushState() changes the url of the iframe source page. To change the main url in the address bar you have to address it to the parent so use this

window.parent.history.pushState();

You should just need to keep the function called wrapped in the click event parenthesis...

$('a[href]').click(function(e){
    e.preventDefault();
    newHref = $(this).attr('href');
    history.pushState('', '', newHref);
});

本文标签: jqueryjavascript historypushState not working for iframeStack Overflow