iFrame is killing javascript

Okay Guys,
This is really weird.
I created a page that is meant to prevent the visitor from using the Browser's Back Button.
And it functions very nicely in both FireFox and Safari.

You can see it working properly here: "http://www.stopsnoopers.com/DetectBACK/DetectGoBACK.html"

But once I place the exact same page within an iFrame, it fails to perform at all.
You can see how it's not working here: "http://www.stopsnoopers.com/DetectBACK/DetectGoBACKiFrame.html"

It's the identical page. So why is it failing once placed in an iFrame? And how can it be fixed to function as intended in the iFrame?

Any ideas? Any solutions?
Thanks,
Sas
LVL 1
sasnaktivAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
What is die() supposed to be doing - that is a PHP function
0
sasnaktivAuthor Commented:
I picked up "die()" from a javascript tutorial.
Should I delete the else statement altogether?
0
GaryCommented:
Well they probably had a function called die, just remove it.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

sasnaktivAuthor Commented:
Hi Gary,
First I removed the die() alone == no change in performance.
Then I removed the entire else statement == no change in performance.

But it still functions properly outside of the iFrame.
What do you think?
Sas
0
GaryCommented:
I guess the persistence only applies to the parent page and not the iframe which is a subset of the page and doesn't have it's own cache - I'm not 100% sure.

I'll have a ponder...though I don't see the purpose of something if it doesn't work cross browser.
0
JohnBusiness Consultant (Owner)Commented:
As a user, I get IFRAME errors (now) only from Mail.com. As such, I stopped using Mail.com with Windows 8 / IE11.

I used to get a lot of IFRAME errors, and assuming (1) all sites cannot be bad and (2) some site corrupted my IE11, I did a full reset of IE11 and also disabled Java add-ins in IE11. Reliability went up enormously and only Mail.com is a problem. It has to be their fault.
0
Dave BaldwinFixer of ProblemsCommented:
Your iframe code is not a valid HTML page.  You are missing the </head> tag and maybe some other things.  That can affect the operation of the page.  You should make it a valid page before you decide whether it works or not.

And there is no reason that javascript (in general) should not work in an iframe.  An iframe is another web page that is displayed inside the iframe.  Doesn't mean that you can't write a javascript function that doesn't work...
0
sasnaktivAuthor Commented:
Well Dave, it does now, and that has no effect.
And I agree, that's why I placed a conspicuous java Back Button on the page for a test.
Sas
0
Dave BaldwinFixer of ProblemsCommented:
0
sasnaktivAuthor Commented:
Yeah Dave,
My HTML may not always be proper. Or maybe just dated. And I don't necessarily think the "validator" is correct in some of it's error flags.

But as you say "It may not make a difference".
And it indeed does not have any bearing on the issue at hand. Simply because any errors are constant regardless of whether the page is being presented by itself (where the javascript functions properly) or in an iFrame (where the javascript fails).
It should work either way, right?

Please understand that I appreciate your input.
So, what's causing it to fail in frames???
Sas
0
Dave BaldwinFixer of ProblemsCommented:
I don't know yet.  I didn't see anything yet in the Firefox Error Console that would tell me anything.

But I do know that browsers work better with proper code.  And bad formatting or missing tags can cause them to interpret things 'wrong' meaning not as you want.
0
JohnBusiness Consultant (Owner)Commented:
Yeah Dave,   My HTML may not always be proper. Or maybe just dated   <-- Yes, and so I as a user with Windows 8 / Windows 10 and IE11 suffer with a barrage of IE and Java "Stopped working".  I know I am not a Java programmer and did not enter the discussion this way. If you are looking for a way to keep me away from your site, you succeeded marvelously well.
0
GaryCommented:
Wouldn't it be easier to just check a cookie
On page load check if cookie is set - if not then set the cookie and load the page
If cookie is set then redirect.
Or using the same logic local storage (but that doesn't work in Opera Mini)
0
sasnaktivAuthor Commented:
Hi Gary,
Setting a cookie as you suggest might indeed be easier, if I knew how to do that.
I don't.
Can you help with that?

Then again, do you think I'd have the same problem when the page is in an iFrame?
Sas
0
GaryCommented:
http://www.w3schools.com/js/js_cookies.asp
The iframe would have it's own cookies seperate from the parent so would be no problem.
0
Michel PlungjanIT ExpertCommented:
Only if they are not from the same origin or the path is set to something else than /
If you just set a cookie, the parent window can read it too
0
Dave BaldwinFixer of ProblemsCommented:
You should note that any of these methods if they work, not only prevent the person from going 'back' to that page, they also prevent them from going to that page to order another item.
0
sasnaktivAuthor Commented:
Thanks for the heads up Dave. I guess I should not waste anymore time trying to learn how to make this work with cookies. Right?

I have found that the javascript I've presented to you in "http://stopsnoopers.com/DetectBACK/DetectGoBACK.html" functions properly and depending on where the Java Alert is targeted the customer can indeed buy more of whatever is for sale. The page functions just fine when it's out of frames.

The problem is once it's in an iFrame (which the page must be eventually) "http://stopsnoopers.com/DetectBACK/DetectGoBACKiFrame.html" it just doesn't present the Java Alert and therefore fails. It's as though the script is not being called on by the addEventListener

So, I'm thinking (maybe wrongly, again) that the problem is with the addEventListener. Is there another way to fire up the script when the BACK button is pressed?

Here's another thought -- When a PHP page (that's what this will be eventually) is reloaded by a pressed BACK Button -- can that reloaded PHP page actually reload new updated data from MySQL, or is it "stuck" with the initial $variables it had the first time it was parsed? Or can it count ($count<'0';) how many times the page was reloaded, then change the initial $variables?

Am I asking too many questions for one day?
I hope there's a "yes" answer to more than this last question.
Thanks Guys,
Sas
0
GaryCommented:
What is the point of the iframe?
0
sasnaktivAuthor Commented:
Navigation control
0
GaryCommented:
Using frames for navigation died out in the last century and I'm sure there is a law against it
The better choice is to use AJAX
0
sasnaktivAuthor Commented:
I don't know AJAX and I can't rewrite a site that consists of hundreds of pages.
So, I'm stuck with the frames (which does not capture any external pages) like it or not.

But what do you guys think about some of the questions I raised?
Sas
0
Dave BaldwinFixer of ProblemsCommented:
Did the use of 'sessions' solve the initial problem where people were getting things for free?
0
sasnaktivAuthor Commented:
Unfortunately it did not, Dave.
I had rewritten all the pages involved to include 'sessions' with no effect.

I just can't understand why the JavaScript fails when in a frame of any kind.
Sas
0
sasnaktivAuthor Commented:
Okay Guys,
I think I may have solved it using a totally different approach that's in one of my recent questions.
I need to do some testing and I'll let you know the outcome--maybe tomorrow.
Thanks
Sas
0
sasnaktivAuthor Commented:
As promised the solution was the answer to one of my previous questions.
When a PHP page (that's what this will be eventually) is reloaded by a pressed BACK Button -- can that reloaded PHP page actually reload new updated data from MySQL, or is it "stuck" with the initial $variables it had the first time it was parsed?

The answer is: Yes!
When a PHP page is reloaded via the Browser's Back Button it can indeed reload new data from MySQL. It is not "stuck" with the values from the initial parse as I thought it would be.

So, when the customer reaches the page that follows the transaction & delivery of goods, simply UPDATE the customer's record in your DB column (e.g.: 'PAID') with a value indicating that fact (e.g.: 'yes'). Then on any previous page that the customer might GO BACK TO place the following code:
$queryPAID = "SELECT PAID FROM YourTable WHERE CustomerUniqueIdentifier ='$CustomerUniqueIdentifier' "; 
$resultPAID = mysql_query($queryPAID) or die(mysql_error());
while($row = mysql_fetch_array($resultPAID)){
$PAID = $row['PAID'];} //$SessionPAID='yes';
if($PAID=='yes'){
echo '<script type="text/javascript">window.alert("THIS eMAIL SESSION HAS BEEN TERMINATED!\n\nREASON: You can not use the Browser\'s Back Button.\nPlease Log-In again.")
location.href = "LogIn.php ";</script>';
   exit;
} 

Open in new window


It works beautifully!
But I still don't know why the original version of Javascript fails within frames and this approach doesn't.

Thanks for the help everyone,
Sas
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.