2

I'm trying to do is load an external html page then display the contents within a div when a button is clicked.

I get the alert message displaying the contents of the test.html, however when I try to display it within a div nothing happens.

The contents is just a div tag. I don't want to load the div tag just the text itself which does come up in the alert message.

Within a html file, I know I don't really need to use ajax for something so simple however if I can get this to work it will help me with future problems.

      $.ajax({
      type: "GET",
      url: "test.html",
      dataType: "text",
      success : function(data) {
                    step1j = data;
            alert(step1j); // WORKS
            return step1j;
                }
    });


$("#step1").click(function() {
    $("#texter").html(step1j);
});
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Codegenesis G
  • 79
  • 1
  • 1
  • 9

2 Answers2

4

Ajax calls are asynchronous by default so what you have won't work. What I would suggest is using jQuery's .load() which handles the entire loading into a div for you (jQuery reference info here).

$("#step1").click(function() {
    $("#texter").load("test.html");
});

For this to work, the following must be true:

  1. #step1 must exist already at the time you run this initial code (e.g. the DOM must already be loaded to install the .click() handler. For example, you can't install a click handler from code in the <head> tag because the page DOM has not yet loaded.
  2. #texter must exist at the time of the click. Please make sure there is no misspelling there.
  3. The URL you are loading must be of the same origin (e.g. same domain) as the web page where you are running the javascript. This is because of the brower's "same origin" security restrictions.

Read this answer if you want to learn more about why you can't return data from an asynchronous Ajax call like you were trying to do. Asynchronous calls finish some indeterminate time later and thus the only reliable place to use their results is in the completion callback or in a function that you call from within the completion callback. In this case, .load() is a higher level piece of jQuery functionality that makes this easier.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thanks, I tried .load but it still didnt update the data within the div. Do I need to declare a variable with that holds the contents of test.html then display it? – Codegenesis G Sep 05 '14 at 00:30
  • @CodegenesisG - if your URL is the same origin as your page (e.g. same domain, port and protocol), then `.load()` just as I've shown it works perfectly fine all by itself. If your URL is not the same origin, then you can't fetch that content with ajax at all because of the brower's "same origin" security restrictions. If you are loading from the same origin and it still isn't working, then something else you haven't disclosed is wrong because this type of code works just fine. – jfriend00 Sep 05 '14 at 00:31
  • Do i still need the ajax chunk of code for this to work? – Codegenesis G Sep 05 '14 at 00:37
  • @CodegenesisG - Please read the [jQuery doc page](http://api.jquery.com/load/) I linked above and here again. It is all spelled out there. As I say in my answer `.load()` will do it all for you with no other ajax call. Is your div `#texter` or `#tester` as that typo could cause it to fail? – jfriend00 Sep 05 '14 at 00:49
  • Thanks im checking them now. I think the problem might be with the actual test.html file – Codegenesis G Sep 05 '14 at 01:05
  • I didn't realise it loaded the contents of the htmls div not just all of what was within the html file which was why the div was displaying nothing. Really thankful for all your help man its really really appreciated!!!! THANK YOU SO MUCH! – Codegenesis G Sep 05 '14 at 01:13
1

Try this:

$("#step1").click(function() {

    $.ajax({
      type: "GET",
      url: "test.html",
      dataType: "text",
      success : function(data) {
            $("#texter").html(data);
            //step1j = data;
            //alert(step1j); // WORKS
            //return step1j;
      }
});
});
artm
  • 8,554
  • 3
  • 26
  • 43