Skip to content

Instantly share code, notes, and snippets.

@robcolburn
Last active December 19, 2015 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robcolburn/5969440 to your computer and use it in GitHub Desktop.
Save robcolburn/5969440 to your computer and use it in GitHub Desktop.
External JS w/ jQuery Data Attributes
$(document).ready(function() {
var data = $('body').data();
$("#thing-1").load(data.path + "resource-1");
$("#thing-2").load(data.path + "resource-2");
});
<? $path = "http://www.example.com/"; ?>
<body data-path="<?= $path ?>">
<div id="thing-1" class="thing">
</div>
<div id="thing-2" class="thing">
</div>
<script src="after.js"></script>
</body>
$(document).ready(function() {
$('.thing').each(function () {
$(this).load( $(this).data("path") );
});
});
<? $path = "http://www.example.com/"; ?>
<body>
<div id="thing-1" class="thing" data-path="<?= $path ?>resource-1">
</div>
<div id="thing-2" class="thing" data-path="<?= $path ?>resource-2">
</div>
<script src="after.js"></script>
</body>
<? $path = "http://www.example.com/"; ?>
<body>
<script>
var path = "<?= $path ?>";
</script>
<div id="thing-1" class="thing">
<script type="text/javascript">
$(document).ready(function() {
$('#thing-1').load(path + "resource-1");
});
</script>
</div>
<div id="thing-2" class="thing">
<script type="text/javascript">
$(document).ready(function() {
$('#thing-2').load(path + "resource-2");
});
</script>
</div>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment