I have a static html document with an iframe in its html code as this
<iframe src="http://192.168.1.1/test.php"></iframe>
and test.php simply echoes a line of text:
<?php
echo '
<div class="notice">
Welcome My friend
</div>';
?>
What's strange however is that the iframe loads redundant code which I didn't add and which breaks the final DOM with a nested element:
<html>
<head>
</head>
<body>
<div class="login">...</div>
</body>
</html>
How can I avoid this extra unrequested html from being generated?
Iframe is used to embed another document within the current HTML document. So it is normal behavior of iframe tag. And you cannot handle [using CSS] or modify html code which rendered from iframe. So may be that causes your page is breaking.
Related
I need to load, modify and output some 3rd party iframe on my webpage. As suggested, I had created the intermediary page that simply contains <iframe> with its src attribute.
The php code that outputs the page looks like this:
$iframe->loadHTML(file_get_contents("http://example.com/iframe_page.php")); //creating DOM object, see htm content below
$xpathObj= new DOMXPath($iframe);//creating DOMXPath object
foreach ($xpathObj->query('//div[#id="specific_id"]') as $node){ //this query returns nothing
$node->parentNode->removeChild($node);//i need to remove the div with that id, but there is nothing to remove
}
echo $iframe->saveHTML($iframe->documentElement);//the iframe output works fine
And the content of my iframe looks something like this:
<html>
<head>
</head>
<body>
<div>
<div>
<div id="specific_id">
</div>
</div>
</div>
</body>
</html>
I've even tried disabling JS to see if this div is placed there by front-end code and nope with JS disabled the structure of the document looks exactly the same.
The answer is rather simple. You CAN'T manipulate iframe element crossdomain using PHP DOMDocument.
I'm trying to use the following code to replace <body> tag from page with <body id="khanqah">
echo str_replace("%body%", "khanqah", "<body id='%body%'>");
It does adds <body id="khanqah"> to the page but the actual <body> tag still presents. I mean there are two body tags now, one <body> and the other <body id="khanqah">
Also the <body id="khanqah"> tag is adding at the top of page, see this: http://i.imgur.com/6zYWTv8.jpg (screenshot of page source)
Is there any way I can work around?
It's not really replacing anything in the HTML, it's just echoing the return value of str_replace("%body%", "khanqah", "<body id='%body%'>") which happens to be the string <body id="khanqah">.
You can only replace the HTML's body element with PHP if you are outputting the HTML with PHP (changing it before outputting it). PHP works server-side, so once the HTML reaches the client it cannot modify it.
You can use JavaScript, which works client-side, to do this.
To change the id of the body dynamically using jQuery (which is the easiest way), you can do
$('body').attr('id', 'khanqah');
How can I include a file, but 'forget' everything about it after it has been included and rendered; contain it, so to speak.
Back in the day, I guess I would have just used a frame but now times have changed. I suppose an iframe is still fair game.... but are there any other ways?
p.s. I can't edit the included file :).
index.php
<?php
echo '<p>This is some left-aligned text</p>';
include 'include.html';
echo '<p>This is some more left-aligned text</p>';
?>
include.html
<html>
<head>
<style>
body{
text-align:center;
}
</style>
</head>
<body>
<p>This is some centered text</p>
</body>
</html>
Output
This is some left-aligned text
This is some centered text
This is some more left-aligned text
Desired Output
This is some left-aligned text
This is some centered text
This is some more left-aligned text
'Ultimate' Objective
Just to give some more information, I have an epub file. I load the container.xml to determine the root file which is a .opf file which tells me the spint, which contains lots of .html file...
I am then including all of these html files into the page so that an epub can be read in one continuous flow.
The epub files cannot be edited? Unless I can do this without modifying the epub files permanently.
You can try to use scoped css, but is an experimental feature.
So in the head of index.php you should include this jquery plugin: https://github.com/thingsinjars/jQuery-Scoped-CSS-plugin
then, to achieve what you want to achieve, you have to change your include.html file to the following:
<html>
<head>
</head>
<body>
<style scoped>
body{
text-align:center;
}
</style>
<p>This is some centered text</p>
</body>
</html>
I think this should do the trick and make you load 'every' include page you want to include without letting its style to influence the rest of the document.. but in the limits of the JQuery plugin you are using (it has some problems).
Chrome and Firefox currently implement this specific feature natively and don't need this plugin.
Load the file as a string and load only the body of the html file, becouse you can't have 2 HTML tags in the same file.
<?php
echo '<p class=\'left\'>This is some left-aligned text</p>';
$html = file_get_contents('include.html');
preg_match('/\<body\>(.*?)\<\/body\>/is', $html, $matches);
echo $matches[1];
echo '<p class=\'left\'>This is some more left-aligned text</p>';
?>
Just create css classes on index.php
<?php
echo '<p class=\'left\'>This is some left-aligned text</p>';
include 'include.html';
echo '<p class=\'left\'>This is some more left-aligned text</p>';
?>
In the style tag add on index.php:
<style type="text/css">
.left{text-align:left;}
</style>
No changes needed to your "included" html file.
And it should now work fine with that include. I will say that an iframe is perfectly fine, depending on what you are doing. The current demo page you have doesn't make a lot of sense, but I assume this is just for testing. So the above should work fine for you.
Is it "okay" to have an HTML document embedded inside the body tag of another HTML document?
The reason why I want to do this is so that I can call a javascript body onload -- I cannot do that in the main HTML document because the main HTML code is dynamically generated by a controller (Yii) that controls other pages and I do not want to edit it.
*By the way, I tried it and it seems to work fine now, but I just want to be sure that the page will not break in the future for whatever reason.
<html>
<head>
<body>
<html>
<head>
<script type="text/javascript>
function somefunction(){
}
</script>
</head>
<body onload="javascript:somefunction()">
</body>
</html>
</body>
</html>
If all you want to do is attach an onload event, you're going about it the wrong way.
All you have to do is add a script element that attaches an onload event:
<script type="text/javascript">
function somefunction()
{
...do stuff...
}
document.body.onload = somefunction;
</script>
Alternatively, if you've appended your JS files at the bottom of the page, they will be able to interact with the DOM similarly to how onload works. The reason to use onload is only so that the elements defined within the web page have been added to the DOM by the time a function is executed. If your scripts are after your content, the elements will be in the DOM.
No, that's bad HTML.
Just put your JavaScript at the bottom before </body>.
I just started with php. I have a pretty simple site with header and footer includes and the content in between.
Right now, I have each page getting the title with a variable, like so:
<?php $title = "My Company - New Products"; ?>
However, instead of having that on each page, in the header include, I'd just like to have the title be a standard "My Company - " and then have it pick up each page's H1 tag.
This seems simple, but all my search results have been specific to various CMS's.
Thanks a lot.
Why not define $title and then reference it more than once in the header?
Main page:
<?php
$title = "New Products";
require "header.php";
?>
<p>Rest of page...
Header.php:
<title>My Company - <?php echo htmlentities($title) ?></title>
<h1><?php echo htmlentities($title) ?></h1>
First, PHP has nothing to do with your page's H1 tag or whatever HTML tag, it should just generate a bunch of text, which happen to be a complete HTML document (or not depending upon you...)
You should probably look at templating mechanisms for PHP. Then, you can separate the logic necessary to generate the page content from the presention of this content as HTML. There are template engines like Smarty available, but many people will argue that PHP itself can be perfectly used as a template engine.
The easiest way to use PHP as a template engine is as described by jleedev. The PHP file as requested by the user generates variables (with the content of the page) and shouldn't contain any piece of HTML code (not even inside string variables). It then includes one or more template files, which use this variables to generate all HTML output without changing or computing any data.
I know this question is over a decade old, but here's what I use in 2022.
This will set the page title to the first H1 tag found on the page. If there are no tags, it will use the default title passed to it.
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php echo get_h1_title('My Company','Default Title if no H1'); ?></title>
</head>
<body>
<h1>H1 Title for this page</h1>
<p>Text for this page</p>
</body>
</html>
<?php
function get_h1_title ($prefix, $default_title)
{
$auto_title = $default_title;
$html = file_get_contents ($_SERVER['SCRIPT_FILENAME']);
if (!empty($html))
{
// Disable errors - DOMDocument doesn't handle any html5 we might use
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
// Get embedded h1 title if available
$header1s = $dom->getElementsByTagName('h1');
if (count($header1s))
{
$auto_title = $header1s[0]->nodeValue;
}
}
return $prefix . ' - ' . $auto_title;
}
?>
If on a given page you don't want the existing H1, just include a non-display H1 with the title you do want:
<body>
<h1 style="display:none">H1 that becomes page title but not displayed</h1>
<h1>H1 Title for this page</h1>
<p>Text for this page</p>
</body>
I agree with jleedev, but I use require() instead of include.
And don't use the shorthand opening php tag (<?)!