Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

javascript - browser back acts on nested iframe before the page itself - is there a way to avoid it?

I have a page with dynamic data loaded by some ajax and lots of javascript.

the page contains a list from which the user can choose and each selected value loads new data to the page.

One of these data items is a url provided to an iframe.

I use jQuery BBQ: Back Button & Query Library to simulate the browser-back behavior.

All works well besides the fact that when i click the back button for the first time the iframe goes back to its previous location and then I need to click back again to make the page go back.

Is there a way to disable the iframe's back behavior?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've found the answer to my problem guess it could be useful for others out there.

The problem was with the way i assigned new URLs to my Iframe, i used Jquery so it looked something like that:

$('#myIFrame').attr('src',newUrl);

When assigning the URL in that manner it adds a new entry to the browser's list of visited URLs to go back to.
That wasn't the desired behavior, so after some googling i found that you can assign a new URL to an Iframe object without adding it to the 'back-list', it looks like that:

var frame = $('#myIFrame')[0];  
frame.contentWindow.location.replace(newUrl);

This way my back button behave exactly as expected.

btw, i got my answer from here.

Hope this was helpful to you as it was to me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...