MaRco
MaRco

Reputation: 45

Ajax response contains the source code of the page and not the final content of the page

I am trying to receive, using ajax, a value from a page. The value is printed only when a specific javascript code is executed.

page1.html:

$.ajax({
   type: "GET",
   url: "./page2.html",
   dataType: "text",
   success : function(data){
      alert ( $(data).filter("#returnvalue").html() );
   }
})

page2.html

<div id='returnvalue'>AAA</div>
<script>
  var cond = true;
  if(cond){
    document.getElementById("returnvalue").innerHTML = "BBB";
  }
</script>

My alert prints "AAA". why?

It's like the javascript in the second page is not executed and i'm only receiving the source of the page and not the content of the page after changes.

What am i doing wrong? Does anyone have ideas/solutions?

Thank you very much.

Upvotes: 1

Views: 1562

Answers (2)

Barmar
Barmar

Reputation: 782508

I think this should do what you want:

success : function(data){
    $("#somediv").html(data); // load the data into the DOM and execute scripts
    alert ( $("#returnvalue").html() );
}

Upvotes: 3

AnandSonake
AnandSonake

Reputation: 577

Write a function in page2 which will contain script(on document ready function). I think, You are not making right use of Ajax response

success : function(data)
{
  $("#returnvalue").html(data);
}

Upvotes: 0

Related Questions