Click here to Skip to main content
13,197,796 members (43,467 online)
Rate this:
 
Please Sign up or sign in to vote.
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()".
iframe.contentWindow.history.back();

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

window.history.back();


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



my StudentList.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
<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
 function GoBack()
{
  window.history.back();
}


Again, my back button is inside my iframe.
Thanks in advance
Posted 1-Jan-17 20:01pm
Updated 1-Jan-17 20:56pm
v2
Comments
ProgramFOX 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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

If the back button is part of the page displayed in the iframe, then you should run this on the onClick event:
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.
  Permalink  
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.
ProgramFOX 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.
ProgramFOX 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
ProgramFOX 2-Jan-17 3:07am
   
Okay. Any errors in the browser console?
Ali Majed HA 2-Jan-17 3:08am
   
No, I have debugged it with firebug, and it says I have no error.
ProgramFOX 2-Jan-17 3:09am
   
Weird. Then it should work.

Note that your closing <p> tag is not really closing. But I don't think this will matter for the JavaScript part.
Ali Majed HA 2-Jan-17 3:11am
   
it is correct in my main code, as I was rushing to type code for you< I had the mistake.
ProgramFOX 2-Jan-17 3:22am
   
What you can try now, is putting a breakpoint in the GoBack function to see if the debugger even gets there. If the function doesn't get executed, then you know one thing more. But based on the code I can see now, it should work, so I can't exactly tell what's going on. It will require some debug work from your side.
Ali Majed HA 2-Jan-17 3:13am
   
Thanks in advanced that you are taking your precious time to solve my problem.
Ali Majed HA 2-Jan-17 3:07am
   
I have no additional code in my StudentDetail.html exept the script to reference the .js file
Ali Majed HA 2-Jan-17 3:56am
   
thanks ProgramFOX, My problem was a long delay in loading iFrame. I wait about 10second to load I frame completely, then my function worked correctly for browsing back the history. I have some problem in my references priority I think, Thanks in Advance for your great help
ProgramFOX 2-Jan-17 4:00am
   
Ah, I see. That's still a very long time... perhaps it's taking long to fetch the data from the database? It's worth taking a look at that.
Ali Majed HA 2-Jan-17 4:10am
   
I will do it as soon as possible,I really appreciate you and your kindness. I am going to optimize my code for this problem.
Thanks again

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.171020.1 | Last Updated 2 Jan 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100