1.11M Members

getElementsByClassName coupled with file_get_contents

 
0
 

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!

 
0
 

Hi,

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

 
2
 

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; ?>
 
0
 

@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.

 
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.

 
0
 

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

 
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.

 
0
 

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.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article