This seems to be a problem related to Safari only. I've tried 4 on Mac and 3 on Windows and am still having no luck.
I'm trying to load an external HTML file and have the JavaScript that is embedded execute.
The code I'm trying to use is this:
$("#myBtn").click(function() {
$("#myDiv").load("trackingCode.html");
});
trackingCode.html
looks like this (simple now, but will expand once/if I get this working):
<html>
<head>
<title>Tracking HTML File</title>
<script language="javascript" type="text/javascript">
alert("outside the jQuery ready");
$(function() {
alert("inside the jQuery ready");
});
</script>
</head>
<body>
</body>
</html>
I'm seeing both alert messages in IE (6 & 7) and Firefox (2 & 3). However, I am not able to see the messages in Safari (the last browser that I need to be concerned with - project requirements - please no flame wars).
Any thoughts on why Safari is ignoring the JavaScript in the trackingCode.html
file?
Eventually I'd like to be able to pass JavaScript objects to this trackingCode.html
file to be used within the jQuery ready call, but I'd like to make sure this is possible in all browsers before I go down that road.
This doesn't seem to work if you're loading the HTML field into a dynamically created element.
I look at firebug DOM and there is no script node at all, only the HTML and my CSS node.
Anyone have come across this?
I have try to make one jquery plugin for html loading function. Please refer the following link.
http://webtech-training.blogspot.in/2010/10/dyamic-html-loader.html
Please suggest me to improve it more.
Thanks, Mohan
I ran into this where the scripts would load once, but repeat calls would not run the script.
It turned out to be an issue with using .html() to display a wait indicator and then chaining .load() after it.
When I made them separate calls, my inline scripts loaded every time as expected.
For further research, note that there are two versions of .load()
A simple .load() call (without a selector after the url) is simply a shorthand for calling $.ajax() with dataType:"html" and taking the return contents and calling .html() to put those contents in the DOM. And the documentation for dataType:"html" clearly states "included script tags are evaluated when inserted in the DOM." http://api.jquery.com/jquery.ajax/ So .load() officially runs inline scripts.
A complex .load() call has a selector such as load("page.html #content"). When used that way, jQuery purposefully filters out script tags as discussed in articles like this one: https://forum.jquery.com/topic/the-load-function-and-script-blocks#14737000000752785 In this case the scripts never run, not even once.
I was able to fix this issue by changing
$(document).ready()
towindow.onLoad()
.Test with this in trackingCode.html: