1

I'm having issues getting jQuery to work correctly while testing on Localhost.

The function that's giving me trouble:

function poll() {
        $.get(location.href, function(data) {
            var x = $('#datadump', data);
            alert(x.html());
        });
    }

Where location.href = http://localhost/polltest.php

The alert merely returns null instead of a random number produced by PHP's rand function. The source of localhost/polltest.php is:

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
        function poll() {
            $.get(location.href, function(data) {
                var x = $('#datadump', data);
                alert(x.html());
            });
        }
    </script>
</head>
<body onload="poll();">
    <div id="datadump">
        <?php
            $val = rand(0, 100);
            echo $val;
        ?>
    </div>
</body>
</html>

Any help regarding a way for this to work would be wonderful and appreciated.

2
  • are you trying to add the result of $.get() to <div id="datadump"> </div> ? Commented Dec 29, 2011 at 7:09
  • At this point I'm only trying to get the innerHTML of #datadump (the output of rand(0, 100); in php). Commented Dec 29, 2011 at 7:13

2 Answers 2

1

There could be multiple ways to achieve the value of the div with id datadump.

One of the ways being

   function poll() {
        $.get(location.href, function(data) {
            x = $(data).filter('#datadump');
            console.log(x);
        });
    }

The reason it is failing for you:

When you have an HTML string which contains <html>, <head>, <body> tags, and you try to do

$(string)

those elements will be ignored. Only those elements which can be put inside a div are valid. Read it in the jQuery documentation.

When passing in complex HTML, some browsers may not generate a DOM that exactly replicates the HTML source provided. As mentioned, we use the browser's .innerHTML property to parse the passed HTML and insert it into the current document. During this process, some browsers filter out certain elements such as <html>, <title>, or <head> elements. As a result, the elements inserted may not be representative of the original string passed.

This issue has been discussed in detail on this link: https://stackoverflow.com/a/5642602/410367

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! This worked. However, I don't believe your reason for failure is correct, as the method I originally tried does work on sites other than localhost.
@AlexL I would like to see it happening elsewhere in that case. I've also added a link to related SO discussion in my reply.
0

Can you try this way,

<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        var x = $('#ads');
        alert(x.html());

         $.get(location.href, function (data) {
            var x = $('#ads', data);
            alert(data);
            alert(x.html());
        });
    });
</script>
</head>
<body>
  <div id="datadump">
    <?php
        $val = rand(0, 100);
        echo $val;
    ?>
  </div>
</body>

Update: I have changed the script little bit similar to your original post. It works fine for me.

1 Comment

That would defeat the purpose of using $.get(), since that only grabs the innerHTML of #datadump that was originally loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.