2

My website uses the iframe to load content inside div element. My problem is when a page is refresh it redirect to the main page not to the current page.

Here is the part of the code.

     <a  href="promotion.jsp">Promotion</a>

And here is the javascript function I used.

$(document).ready(function(){

        var url;
        $('a').click(function(e){

            e.preventDefault();
            url = $(this).attr('href');
            if(undefined != url){
                window.frames["page-wrapper"].src = url;
                clickFront();
            }
        });

        $("button").click(function() {
            checkUrlandId(url,this.id);         
        });

    });

clickFront() is function that enable and disable some top menu bar button functionalities.

So when I click menu item(Approvals) page changed. After that page refreshes it redirect to the main page not to the current page.

Here are two screenshots. First one is before clicking the menu item and second one is after clicking the menu item.

First Image

Second Image

After clicking the menu item url is not changing.

I read that we can use cookies or javascript localstorage to save the url to do this? Can someone help me... I'm a newbie to the js.

2 Answers2

1

I would expect window.frames["page-wrapper"].src = url; to be
window.frames["page-wrapper"].location = url;

Anyway. If you need the iFrame change to survive a refresh and a bookmark, you need to use the hash in the page:

$(function(){
    var url = location.hash?decodeURIComponent(hash.substring(1)):"";
    if (url) window.frames["page-wrapper"].location = url;
    $('a').click(function(e){
        e.preventDefault();
        url = $(this).attr('href');
        if(undefined != url){
            location.hash=encodeURIComponent(url);
            window.frames["page-wrapper"].location = url;
            clickFront();
        }
    });

});

If URLs are external, you really need target.

Localstorage

$(function(){
    var url = localStorage.getItem("url");
    if (url) window.frames["page-wrapper"].location = url;
    $('a').click(function(e){
        e.preventDefault();
        url = $(this).attr('href');
        if(undefined != url){
            window.frames["page-wrapper"].location = url;
            localStorage.setItem("url",url);
            clickFront();
        }
    });

});
mplungjan
  • 155,085
  • 27
  • 166
  • 222
1

Look at sample below, everything works as expected

var anchors = document.querySelectorAll('a');
Array.prototype.forEach.call(anchors, function(a) {
  a.onclick = function(e) {
    e.preventDefault();
    window.frames.frame1.src = a.getAttribute('href');
  }
});
<ul>
  <li>
    <a href="http://stackoverflow.com">Stackoverflow main page</a>
  </li>
  <li>
    <a href="http://stackoverflow.com/users/2308005/medet-tleukabiluly?tab=profile">My profile</a>
  </li>
  <li>
    <a href="http://stackoverflow.com/questions/41629581/node-js-obtain-access-token">Question: Node.js obtain access token</a>
  </li>
</ul>
<hr />
<iframe id="frame1" width="500" height="200" src="http://stackoverflow.com/questions/41629578/when-iframe-page-refreshes-it-redirect-to-the-same-page-not-to-the-main-page"></iframe>
Medet Tleukabiluly
  • 10,703
  • 3
  • 34
  • 62