3

What I'm trying to do is load an external html file and have the Javascript that is embedded execute along with the html code that I had in my Loading_Page.html.

The code I'm trying to use is this:

$("#myBtn").click(function() {
  $("#myDiv").load("Loading_Page.html");
});

Loading_Page.html looks like this (simple now, but will expand once/if I get this working).It has both html and javascript, so I need to get Html page along with the javascript.

<html> 
<head>
  <title>Tracking HTML File</title>
  <script language="javascript" type="text/javascript">
    alert("outside the jQuery ready");
  </script>
</head>

<body>

  <div id="first_division"><img src="someimage.jpg" alt="image" /></div>
  <div id="second_division"><p>some text</p></div>
</body>

</html>

But the javascript in Loading_Page.html page is not executing along with the html code in Loading_Page Any thoughts on why the browser is ignoring the Javascript in the Loading_Page.html file?

Thanks for your help in advance!

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
venkat
  • 31
  • 1
  • 2

1 Answers1

2

jQuery load will just exclude that script tag from response, before it is inserted into a div.

See this question. The same problem happens there.

Community
  • 1
  • 1
Viktor S.
  • 12,736
  • 1
  • 27
  • 52
  • Yeah..I had gone through that question.. Can you please say what to do, to resolve this issue – venkat Oct 02 '12 at 15:08
  • Instead of load - you can try to use `$.ajax` and in success handler - insert returned HTML into `$("#myDiv")` with `.html()` – Viktor S. Oct 02 '12 at 15:11
  • 2
    Something like this: `$.ajax({ url: 'Loading_Page.html', success: function(data) { $("#myDiv").html(data); } });` – Viktor S. Oct 02 '12 at 15:13
  • So can I use anything like $.getScript("javascript.js") as a call back function for load apart from $.ajax to get javascript along with loading html page...?? – venkat Oct 02 '12 at 15:23
  • 1
    Hm. Yes. But actually - I do not think you should do that. This `$("#myDiv").html(data)` or at least `$("#myDiv")[0].innerHTML = data;` should execute JS (in $.ajax success handler). In that case it was not possible to do like that because OP were trying to take only a part of response. – Viktor S. Oct 02 '12 at 15:29
  • So in that case I will use $.ajax({ url: 'Loading_Page.html', success: function(data) { $("#myDiv").html(data); } }); only. Thanks for your valuable reply and patience. – venkat Oct 02 '12 at 15:36
  • Yes. That is what I'm saying about. Just try it on your example code and see if it works. There could be problems with head, body tags inside resulting elements. – Viktor S. Oct 02 '12 at 15:40