We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,145 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

getElementsByClassName coupled with file_get_contents

So here is my current code that doesn't work for what I'm trying to do:

<?php

$urlp1 = "members/";
$urlp2 = 0;
$urlp3 = ".html";
$url = $urlp1.$urlp2.$urlp3;
$data = file_get_contents($url);
?>



<script type="text/javascript">
var url = "<?php echo $url; ?>";
var temp = getElementsByClassName('daniweb');
document.write(temp);
</script>



<p style="margin-bottom: 50px;"></p>
<?php echo $data; ?>

I'm trying to get the echoed $data which has the class name 'daniweb' in it to return in var temp, however even though $data is echoed, its technically not part of the document. Any ideas? Thanks in advance!

4
Contributors
7
Replies
1 Day
Discussion Span
5 Months Ago
Last Updated
8
Views
persianprez
Junior Poster
123 posts since Oct 2006
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

Hi,

Can you please shade more lights upon us. Are you trying to parse the contents of the url? Is that it?

veedeoo
Master Poster
735 posts since Oct 2011
Reputation Points: 298
Solved Threads: 130
Skill Endorsements: 13

That's because you are executing the script before echoing the $data.

If you invert it may work:

<p style="margin-bottom: 50px;"></p>
<?php echo $data; ?>

<script type="text/javascript">
var url = "<?php echo $url; ?>";
var temp = getElementsByClassName('daniweb');
document.write(temp);
</script>

Or, the way I think is better, use the onload event of the window:

<script type="text/javascript">
window.onload = function() {
    var url = "<?php echo $url; ?>";
    var temp = getElementsByClassName('daniweb');
    document.write(temp);
};
</script>

<p style="margin-bottom: 50px;"></p>
<?php echo $data; ?>
AleMonteiro
Master Poster
752 posts since Aug 2010
Reputation Points: 129
Solved Threads: 140
Skill Endorsements: 23

@AleMonteiro the position of the script shouldn't make any difference. Window.onload doesn't do anything either.

@veedeoo I'm parsing the data from the url but also trying to get the data from one of the divs within.

persianprez
Junior Poster
123 posts since Oct 2006
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
var temp = getElementsByClassName('daniweb');

If you paste the contents of the $data var, it might shine some light on this. As for the position of the script, I've moved to placing nearly everything just before the closing </body> tag of the html - far less hassle that way.

however even though $data is echoed, its technically not part of the document

Why not? Whether it's generated by php or 'hard markup' it should still be valid html when it leaves the server - indistingushable form one another.

diafol
Keep Smiling
Moderator
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,511
Skill Endorsements: 57

Ok, I have moved the script to the end of the document.

Here is what is currently in $data

<html>
<body>
<table>
<tr class="daniweb">Hi</div>
<tr class="daniweb">Sup</div>
</table>
</body>
</html>

Even though this is posted into the document, the script cannot pick up the class names from $data

persianprez
Junior Poster
123 posts since Oct 2006
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

Hmm, what browser are you using? If IE8 or previous, you're out of luck I think. It's not reliable: http://caniuse.com/getelementsbyclassname

It works fine in Chrome:

<html>
<body>
<table>
<tr class="daniweb">Hi</div>
<tr class="daniweb">Sup</div>
</table>

<script type="text/javascript">
var temp = getElementsByClassName('daniweb');
document.write(temp);
</script>

</body>
</html>

//EDIT

What I don't understand is that you have <html> and <body> tags in the $data var, but you have other hard-coded html elements outside it. That isn't right.

diafol
Keep Smiling
Moderator
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,511
Skill Endorsements: 57

persianprez,

the order of the script does make difference, if you don't use events to run it. If the script is run before the DOM is created then the elements doesn't exists yet, therefore the method getElementsByClassName() won't find any elements.

AleMonteiro
Master Poster
752 posts since Aug 2010
Reputation Points: 129
Solved Threads: 140
Skill Endorsements: 23

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0780 seconds using 2.72MB