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!

Recommended Answers

All 7 Replies

Hi,

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

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

Member Avatar for diafol
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.

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

Member Avatar for diafol

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.