I am trying to request a page in Ajax and a normal GET request. My basic layout is like this:
<html>
<head>
//css file
</head>
**************
<body>
<script> </script> // this script needs to be loaded at the end of the page load in both GET and AJAX
</body>
*****************
//js files (jQuery)
</html>
THE area in * is loaded by ajax
If I make a GET request, I get the complete page. And if I make an Ajax request, I get only the body part (no jQuery).
Now, the body tag contains a script tag.
<script type="text/javascript">
window.onload = function() {
$.getJSON("/loc", function (data) {
// .......
});
};
</script>
This code runs fine when I make a GET request. This script gets executed after the page loads. But when I make an Ajax request, the script doesn't execute, because the page is already loaded.
So I tried:
<script type="text/javascript">
$(document).ready(function () {
$.getJSON("/loc", function (data) {
.....
});
});
</script>
Now this works fine with Ajax, but doesn't work with a GET request, because jQuery is at the end of the page.
script tag is inside the body tag and the jquery is after the body tag, so if i move it after the jQuery. So I need to make 2 designs for my page: 1 for my Ajax request and 1 for my GET request. If not in Ajax I will end up loading the jQuery script twice.
So my question is can I have a single script that runs for both Ajax and GET requests?