echo <body> doesn't activate jquery script - php

So I have a url from where I extract the data with extracthtml.php:
<?php
include("simple_html_dom.php");
$html = file_get_html($url);
$body = $html->find('body', 0);
$title = $html->find('title', 0);
echo $title;
echo $body;
?>
<script src="extract.js" type="text/javascript"></script>
Then with jquery I put a box around all the p elements to see if this communication works (test, am going to add more css manipulation later). My jquery starts with:
$(document).ready(function(){
$('p').css("border", "2px solid black");
});
Im guessing document.ready is the problem, because there appears to be no boxes around the p elements.

The issue is that you are echoing the jquery line to the browser after you close the page.
Since you echo the $body first, I would guess your page ends up looking something like this:
<body>
...
</body>
<script>
jquery here
</script>
Without seeing your page output this is only a guess, but if it is correct the browser will not run any code after the </body> tag. I would advise checking the source of your output to see if this is the case.

Related

How to open a link which was parsed in a div?

My site parses a spanish dictionary and lets you search for more than one word at a time. If you look up "hola" you get the first div). Some words come up with suggestions, like "casa", instead of definitions like "hola":
And what i am looking for is this:
So, i would like to: when you click on the suggestions (like CASAR in the example I posted) to print the result in a div like HOLA. Here is the code used:
$words = array('word0','word-1');
function url_decode($string){
return urldecode(utf8_decode($string));
}
$baseUrl = 'http://lema.rae.es/drae/srv/search?val=';
$cssReplace = <<<EOT
<style type="text/css">
// I changed the style
</style>
</head>
EOT;
$resultIndex = 0;
foreach($words as $word) {
if(!isset($_REQUEST[$word]))
continue;
$contents = file_get_contents($baseUrl . urldecode(utf8_decode($_REQUEST[$word])));
$contents = str_replace('</head>', $cssReplace, $contents);
$contents = preg_replace('/(search?[\d\w]+)/','http://lema.rae.es/drae/srv/search', $contents);
echo "<div style='
//style
", (++$resultIndex) ,"'>", $contents,
"</div>";
}
I have tried with: $contents .= '' . $word . '<br/>'; But it didn't work nor I know really where/how to use it.
Okay, I'll use jQuery for the example because it will be the easiest to get the job done specially if you are new to programming.
NOTE: I DO NOT RECOMMEND USING JQUERY -BEFORE- LEARNING JAVASCRIPT -- DO IT AT YOUR OWN RISK, BUT AT LEAST COME BACK AND LEARN JAVASCRIPT LATER
First, read up on how to download and install jquery here.
Secondly, you will want something a little like this, let's pretend this is your markup.
<div id="wrapper">
<!-- This output div will contain the output of whatever the MAIN
word being displayed is, this is where HOLA would be from your first example -->
<div id="output">
</div>
<!-- This is your suggestions box, assuming all anchor tags in here will result in
a new word being displayed in output -->
<div id="suggestions">
</div>
</div>
<!-- Le javascript -->
<script>
// Standard jQuery stuff, read about it on the jquery documentation
$(function() {
// The below line is a selector, it will select any anchor tag inside the div with 'suggestions' as identifier
$('#suggestions a').click(function(e) {
// First, stop the link going anywhere
e.preventDefault();
// Secondly, we want to replace the content from output with new content, we will use AJAX here
// which you should also read about, basically we set a php page, and send a request to it
// and display the result without refreshing your page
$.ajax({
url: 'phppage.php',
data: { word: 'casar' },
success: function(output) {
// This code here will replace the contents of the output div with the output we brought back from your php page
$('#output').html(output);
}
})
});
})
</script>
Hopefully the comments will shed some light, you need to then set up your php script which will be sent a GET request. (for example, http://some.address.com/phppage.php/?word=casar)
Then you just echo out the output from PHP
<?php
$word = $_GET['word'];
// Connect to some database, get the definitions, and store the results
$result = someDatabaseFunctionThatDoesSomething($word);
echo $result;
?>
Hope this helps, I expect you have a lot of reading to do!

How to display title of page in body content under h1 tag

i have used RSSEO plugin for optimizing my joomla site, however i want my h1 tag in custom components and pages to be similar to page title. Tried below
<h1>
<script type="text/javascript">
<!--
document.write(document.title);
//-->
</script></h1>
The above script is able to display h1 tag, but when checks source code its not seo friendly as display script
I think i need server side php code, have tried using
<h1><?php echo $PageTitle ?></h1>
But above is not displaying any value. only leading to blank h1 tags
Can anyone suggest and advise pls to do it effectively
thanks
Try this:
HTML:
<h1 id="pagetitle"></h1>
JavaScript:
document.getElementById('pagetitle').innerHTML = document.title;
If you want the script inline:
<h1 id="pagetitle"></h1>
<script>
document.getElementById('pagetitle').innerHTML = document.title;
</script>
Joomla $document will contain the title, so simply inject it in your component / template:
<?php
$document = JFactory::getDocument();
echo "<h1>".$document->getMetaData('title')."</h1>";
?>
this should do the trick.
Try this :
<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$page = fread(fopen($url, "r"), 2048); // first 2KB
if(preg_match("/<title>(.+)<\/title>/i",$page,$result))
{
echo "The title of $url is $result[1]</b>";
}
else
{
echo "The page doesn't have a title tag";
}
?>
This will load faster as you want :
This works in Joomla! 2.5.x:
<?php
$document = JFactory::getDocument();
?>
<h1><?php echo $document->getTitle(); ?></h1>

how to display an array content inside CKeditor?

I need to display the content of doc file inside CKeditor.
I read the content of doc file & passing it into an array line by line :
$rs = fopen("text.doc", "r");
while ($line = fgets($rs, 1024)) {
$this->data[] = $line . "<BR>";
}
then I create an instance of CKeditor:
include_once("ckeditor/ckeditor.php");
$CKeditor = new CKeditor();
$CKeditor->basePath = '/ckeditor/';
foreach ($this->data as $value) {
//what should I write here
}
$CKeditor->editor('editor1');
the CKeditor work right now & appear on my webpage .. but without any content ?
what should I right inside the foreach to passing array content into the editor ?
please help =(
.doc files are zipped up and cannot be read like this, by line. Consider using PHPWord to get access to the contents inside.
EDIT: Looks like PHPDoc can only write and not read, upon further investigation.
PHP tools are very deficient in this area. Your best bet is to use something like DocVert to do your file conversions on the command line. THEN you could load that document inside CKEditor.
EDIT: after OP's comment:
let's consider it's a txt file ... I need the Ckeditor method
Load your decoded HTML content into a Textarea, and give this textarea an HTML ID or class:
$textarea_content = htmlspecialchars_decode(file_get_contents('text.doc'));
Then, in your HTML, call the CKEditor inside a JavaScript tag to replace the textarea with the editor:
<html>
<head>
<!-- include CKEditor in a <script> tag first -->
<script type="text/javascript">
window.onload = function()
{
CKEDITOR.replace( 'editor1' );
};
</script>
</head>
<body>
<textarea id="editor1" name="editor1"><?php echo $textarea_content ?></textarea>
</body>
The documentation page has a lot more details.

Create DOM elements using Javascript

Hi I am trying to retrieve RSS feed using PHP and javascript.
I can get RSS feeds by using PHP but I want to create DOM elements for RSS feed using javascript. I am not sure how to complete this. Could anyone share some tips? Thanks a lot.
PHP
function parse_rss_feed($url){
$contents= file_get_contents($url);
$xmlStr= simplexml_load_string($contents);
return $xmlStr;
}
function get_rss_feed($xmlStr){
echo '<ul>';
foreach ($xmlStr->item as $node):
//I want to transfer my RSS value to my javascript....
$title=$node->title;
$author=$node->creator;
$desc=$node->description;
endforeach;
echo '</ul>';
}
html
<head>
<script type="text/javascript" src="js/slideshow.js"></script>
</head>
<body>
<?php
include 'getFeed.php';
parse_rss_feed('http://myRSSFeed')
get_rss_feed($xmlStr)
?>
</body>
slideshow.js
var rss=document.createElement('artical');
//How do I get the value from my php...
rss.innerHtml=.......???
Couldn't be easier, createElement().
var el = document.createElement('div')
Edit:
To mix JavaScript with PHP just escape it as you'd usually do but I suggest if you're doing something serious to learn about AJAX.
?>
<script>
var foo = <?php echo 'baz' ?>
</script>
<?php

How do i call the css for the page that i get from file_get_content

I am using the following code
<div id="content"> <?php
$homepage = file_get_contents("www.yahoo.com");
echo $homepage;
?></div>
The page appears fine but its without the style sheet. How do i call those style sheets ?
and the link which appears are not Valid (it appears through my domain )
How do i fix it ?
The code that i was using for iFrame which is not working is below
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
var iframe = document.getElementById("frm");
selection = getIframeSelectionText(iframe);
alert(selection);
if(selection.length >= 3)
{ el = $('body', $('iframe').contents());
el.html(el.html().replace(selection, "<span class='highlight'>" + selection + "</span>"));
}
});
});
function getIframeSelectionText(iframe) {
var win = iframe.contentWindow;
var doc = win.document;
if (win.getSelection) {
return win.getSelection().toString();
} else if (doc.selection && doc.selection.createRange) {
return doc.selection.createRange().text;
}
}
</script>
If stylesheet URLs in the page are absolute, everything should display correctly. Otherwise you will have to modify their URLs in the page code (either str_replace or regular expressions).
Also, you can expect at least some styles of the page to break, since you are embedding the whole HTML in an "alien" HTML structure. Generally, HTML tag should always be the out-most tag in the page.
I doubt you will come up with solution which works flawlessly for all websites. Why don't you use IFRAME instead?
This isn't a perfect approach, but you could make use of the <base> tag to change relative URLs so that they match against the domain you're fetching from; that should instruct the browser to load relative URLs.
<div id="content">
<base href="http://www.scraped-domain.com/" />`
<?php
$homepage = file_get_contents("http://www.scraped-domain.com/");
echo $homepage;
?></div>
<base href="http://your-domain.com" /> <!--reset after outputting the contents, so that your page resumes treating your relative URLs to your domain. -->
(Untested.)

Categories