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.
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:
1https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#Attributes