0

I'm a JS novice so be gentle.

I am working on a simple script that reads a value from another web page on my local Intranet.

Intranet Home Page (index.php):

<div id="username">my.username</div>

script.js:

$(document.body).load('http://intranet/index.php #username');

Now, I was wondering, instead of just displaying this value, how do I store it in a variable?

4
  • 1
    jQuery's .get() should help you. Also, Javascript != jQuery Commented Sep 14, 2016 at 9:52
  • 1
    you want to store the username to a variable Commented Sep 14, 2016 at 9:53
  • @AnuradhS That is correct. I am able to load that data, but rather than load it - I'd just like to store it in a variable for using further down in my script. Commented Sep 14, 2016 at 9:54
  • 1
    var username = $("#username").text(); Commented Sep 14, 2016 at 9:55

1 Answer 1

3

You want to query it with ajax, then parse that HTML, then find that element in the parsed HTML and extract its text:

$.ajax({
    url: 'http://intranet/index.php',
    success: function(html) {
        var nodes = $.parseHTML(html);
        var username = $("<div>").append(nodes).find("#username").text();
        console.log(username);
    }
});

Here's an example of the parsing/finding part using a static HTML string:

var html = '<html><body><div>foo<div id="username">my.username</div></div></body></html>';
var nodes = $.parseHTML(html);
var username = $("<div>").append(nodes).find("#username").text();
console.log(username);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Or without the wrapper div:

// Test finding it nested
test('<html><body><div>foo<div id="username">my.username</div></div></body></html>');

// Test finding it top-level
test('<div>foo</div><div id="username">my.username</div><div>bar</div>');

function test(html) {
  var username = null;
  $.parseHTML(html).some(function(node) {
    if (node.id === "username") {
      username = $(node).text();
      return true;
    }
    node = $(node).find("#username");
    if (node[0]) {
      username = node.text();
      return true;
    }
  });
  console.log(username);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

You can see why I used the wrapper div. :-)

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

7 Comments

Thanks for the edit - it now works and displays the value in the console. The only problem is when I try to associate it, it doesn't work. Here is my current code: $.get('http://intranet/index.php', function(data){ $.ajax({ url: 'http://intranet/index.php', success: function(html) { var nodes = $.parseHTML(html); var username = $("<div>").append(nodes).find("#username").text(); } }); document.getElementById("username").value = username; });
With the above code, it sets [object HTMLInputElement] as the value of username instead of the username in my page.
@michaelmcgurk: The variable in your success function is completely private to your success function. The username outside your success function is the automatic global the browser created because you have an element with id="username" in the current page as well as the one you're loading. Move the code using username into the success function, as demonstrated in the answer. Because A) Then you're using the right username, and B) ajax is asynchronous (more).
Thanks so much for the detailed comments and answers. It's really helpful for me to learn. I'm a little unclear what the final script should look like. Is there any chance you could show me how I include the document.getElementById("username").value = username; portion in your Answer.
@michaelmcgurk: Put it where I've done console.log(username).
|

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.