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!

0

1 Answer 1

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.

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

7 Comments

Yeah..I had gone through that question.. Can you please say what to do, to resolve this issue
Instead of load - you can try to use $.ajax and in success handler - insert returned HTML into $("#myDiv") with .html()
Something like this: $.ajax({ url: 'Loading_Page.html', success: function(data) { $("#myDiv").html(data); } });
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...??
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.