Click here to Skip to main content
15,889,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello

I have an iframe in my html page and I have a button in the iframe. I want towhen press the button, iframe source get one step back.

for example if the first soure is google.com and I change the source to w3schools.com by pressing "Visit:w3schools pleas", when I press back button, the iframe source get back to google.com

What I have tried:

I have tried some these codes in my javascript function as back button "onClick()".
JavaScript
iframe.contentWindow.history.back();

JavaScript
document.getElementById("myIframeID").contentWindow.history.back();

JavaScript
window.history.back();


my Main.html
HTML
<iframe src="StudentList.html">



my StudentList.html
HTML
<!-- There is a list of student that I get from my database and make the list
      dynamically by createElement("LI") that you can click on every student to
      see the details, and you will be redirected to StudentDetai.html
      by changing the iframe source-->


my StudentDetail.html
HTML
<h1> Student Info </h1>
<p id="Detail"><p> <!-- This Paragraph ill be filled when user click on a student
           to see his detail, it will be filled from data base -->
<input type="button" value="GoBack" onClick="GoBack();">


my StudentDetail.js
JavaScript
 function GoBack()
{
  window.history.back();
}


Again, my back button is inside my iframe.
Thanks in advance
Posted
Updated 1-Jan-17 20:56pm
v2
Comments
Thomas Daniels 2-Jan-17 2:05am    
Do you mean that the back button is a part of the page that's displayed in the iframe? Or is it still part of the main page, but displayed on the iframe?
Ali Majed HA 2-Jan-17 2:21am    
Hello, I mean it is part of the page that's displayed in the iframe.

1 solution

If the back button is part of the page displayed in the iframe, then you should run this on the onClick event:
JavaScript
window.history.back();

I see that this is in your "What I have tried" list... but I expected this to work and when I tried it, it did.
 
Share this answer
 
Comments
Ali Majed HA 2-Jan-17 2:34am    
Thanks for your Help, but as you have mentioned, I have tried this. I am sure about my onClick function.It works for some other actions like alert(); but it will not work for backing the history.
Thomas Daniels 2-Jan-17 2:42am    
Could you share your current code to handle the onClick?
Ali Majed HA 2-Jan-17 2:58am    
I have shared it by Updating the question. sorry for my delay, the code is in tfs and I have to write code here, I can not copy them.
Thomas Daniels 2-Jan-17 3:00am    
Could you share the full StudentDetail.html? I can't see where the <script> tag is, and this tag might matter to solve the problem. (I don't think I need the full <body>, but at least the <script> tag would be helpful).
Ali Majed HA 2-Jan-17 3:06am    
I have coded my javascript in .js file and used the link in StudentDetail.html. the function works fine for other codes. I din NOt used any script tag in StudentDetail.html exept to reference to StudentDetail.js where I have the Back function

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900