使用iframe

时间:2017-03-01 17:42:34

标签: javascript php redirect iframe clear

我想使用Javascript或PHP清除网页。

但是,我认为该页面的一个区域是:data-form="manual iframe"

所以,当我尝试"清除"该页面只会清除该iframe中的数据。剩下的内容仍然存在。

有谁知道如何清除页面上的所有内容,而不仅仅是iframe中的内容?

我的总体目标是清除当前网页上的所有内容,然后将用户重定向到完全不同的网页(主页)。

到目前为止,这是我的代码(在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;

如果你想看到整件事,它从这里开始: 点击此处:http://challengehut.com/TheChallenges/ (然后点击提交按钮)。在&#34;建议之后&#34;部分,我希望它将用户重定向到网站的主页。

1 个答案:

答案 0 :(得分:0)

在iframe的脚本中,您可以使用window.parent检查代码是否在iframe中:

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

然后基于此,可以相应地设置位置:

header()

因此,使用PHP代码,您可能只需要在脚本标记(在简单的html页面中)中呈现它,而不是使用<? 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>'; ?> 函数:

<script>

this plunker中查看此操作。单击(在iframe内部)两个按钮,以查看它们如何以不同方式加载页面。

注意:

示例中使用 type 属性替换示例public static void callservice() throws URISyntaxException { String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"; HttpClient httpClient = new DefaultHttpClient(); try { HttpPost post = new HttpPost(); URI as = new URI("http://172.24.1.137:8280/finalApi/v1.0.0"); post.setURI(as); post.setHeader("Authorization", "Bearer 62fbb099-af9c-35a4-b8e5-82adf92ae9bd"); post.setHeader("SOAPAction", "http://tempuri.org/IService/accountbalance"); post.setHeader("content-type", "text/xml; charset=utf-8"); post.setHeader("cache-control", "no-cache"); List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); StringBuffer xmlString = new StringBuffer(); xmlString .append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">"); xmlString.append("<soapenv:Header/>"); xmlString.append("<soapenv:Body>"); xmlString.append("<tem:accountbalance>"); xmlString.append("<tem:accountNo>051000207960141</tem:accountNo>"); xmlString.append("</tem:accountbalance>"); xmlString.append("</soapenv:Body>"); xmlString.append("</soapenv:Envelope>"); String stw = new String( "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">\r\n <soapenv:Header/>\r\n <soapenv:Body>\r\n <tem:accountbalance>\r\n <!--Optional:-->\r\n <tem:accountno>051000207960141</tem:accountno>\r\n </tem:accountbalance>\r\n </soapenv:Body>\r\n</soapenv:Envelope>"); urlParameters.add(new BasicNameValuePair("xmldoc", xmlString .toString())); System.out.println(xmlString); // urlParameters.add(new BasicNameValuePair("xml", // xmlString.toString())); post.setEntity(new UrlEncodedFormEntity(urlParameters)); // Execute HTTP request HttpResponse httpResponse = httpClient.execute(post); System.out.println("----------------------------------------"); System.out.println(httpResponse.getStatusLine()); System.out.println(httpResponse.getStatusLine().getStatusCode()); System.out.println(httpResponse.getParams()); System.out.println("----------------------------------------"); // Get hold of the response entity HttpEntity entity = httpResponse.getEntity(); // If the response does not enclose an entity, there is no need // to bother about connection release byte[] buffer = new byte[1024]; if (entity != null) { InputStream inputStream = entity.getContent(); try { int bytesRead = 0; BufferedInputStream bis = new BufferedInputStream( inputStream); while ((bytesRead = bis.read(buffer)) != -1) { String chunk = new String(buffer, 0, bytesRead); System.out.println(chunk); } } catch (IOException ioException) { // In case of an IOException the connection will be released // back to the connection manager automatically ioException.printStackTrace(); } catch (RuntimeException runtimeException) { // In case of an unexpected exception you may want to abort // the HTTP request in order to shut down the underlying // connection immediately. post.abort(); runtimeException.printStackTrace(); } finally { // Closing the input stream will trigger connection release try { inputStream.close(); } catch (Exception ignore) { } } } } catch (ClientProtocolException e) { // thrown by httpClient.execute(httpGetRequest) e.printStackTrace(); } catch (IOException e) { // thrown by entity.getContent(); e.printStackTrace(); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpClient.getConnectionManager().shutdown(); } } 标记中的语言属性,给定该属性的MDN documentation

screenshot from MDN script page   1

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