0

I want to clear a web page using Javascript or PHP.

However, I believe one area of the page is a: data-form="manual iframe"

So when I try to "clear" the page, it only clears the data in that iframe. The rest of the content is still there.

Does anyone know how to clear ALL the content on the page, not just what is in the iframe?

My overall objective is to clear all of the content from the current webpage, and then redirect the user to a totally different web page (the home page).

Here is my code so far (in PHP):

// An attempt using javascript (didn't work, only cleared info in iframe)
echo "<script language=\"javascript\">

var body = document.getElementById('body');
body.innerHTML ='';

var divrow1 = document.getElementByClassName('row');
divrow1.innerHTML ='';
</script>
";

// Another attempt using PHP (didn't work only cleared content in iframe)
document.write ("");

//This is where I want page to redirect to:
header("Location: http://www.challengehut.com/index.php");
exit;

If you want to see the whole thing, it starts here: Click here: http://challengehut.com/TheChallenges/ (then hit the submit buttons). After the "Suggestions" section, I want it to redirect the user to the Home page of the website.

braX
  • 11,506
  • 5
  • 20
  • 33
Andrea
  • 33
  • 4
  • See [How to clear the contents of an iFrame from another iFrame](http://stackoverflow.com/questions/33645685/how-to-clear-the-contents-of-an-iframe-from-another-iframe/33649349?s=1|1.1516#33649349) – guest271314 Mar 01 '17 at 18:04
  • Thank you for this. My main goal is to just redirect the user to a new page. And I want that new page to fill up the >entire< web page (not just the small iFrame). Is there a simple way to do this? – Andrea Mar 01 '17 at 21:48
  • @Andrea did you see my answer? Let me know if you have any questions. – Sᴀᴍ Onᴇᴌᴀ Mar 03 '17 at 19:56

1 Answers1

0

In the script of the iframe, you can check if the code is inside an iframe, using window.parent:

if (parent.window != window) {
    //inside iframe
}

Then based on that, the location can be set accordingly:

if (parent.window != window) {
    parent.window.location = 'http://www.challengehut.com/index.php';
}

So with PHP code, you might just have to render this in a script tag (in a simple html page) instead of using the header() function:

<?
echo '<html><body><script type="text/javascript">
        if (parent.window != window) {
             parent.window.location = "http://www.challengehut.com/index.php";
         }
         window.location = "http://www.challengehut.com/index.php";
</script></body></html>';
?>

See it in action in this plunker. Click (inside the iframe) the two buttons to see how they load the pages differently.

Note:

The language attribute in your example <script> tag was replaced in the example with the type attribute, given the MDN documentation for that attribute:

screenshot from MDN script page 1


1 https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#Attributes

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58