Reputation: 35958
I will be redirecting the user to a page. This page gets all of its content from a REST api in JSON format. So on page load I would like to execute the $.get()
request and load the contents of my divs in the page.
I know how to execute the get
request, however, I don't know how to do it on page load. I have a application.js
file for my entire application. So I can't put it in document.ready
because that it would load on each page in my application.
I will be executing the get
request like this:
$.get(
$(this).data('myurl'),
function (data) {
var item = data.response.item[0];
$('mydiv').html(item.text);
}
);
Upvotes: 0
Views: 4472
Reputation: 5258
From what I understand, you only want to get data from rest api on a specific page..
for that you can do:
$(function(){
if (window.location.href.indexOf('/your/page/url') > -1){
// your $.get here
$.get('/torestapi', function(){ //update divs });
}
});
Upvotes: 1
Reputation: 71918
Add it as an inline script block with a separate document.ready, or just before </body>
.
Or add it to a separate js file, and link it only from the specific page where you want to use it.
Upvotes: 0
Reputation: 28131
With jQuery this is very easy, but you have to put this block in your 'start'-page:
$(function() {
// this code will be executed on page load
app.start();
});
Upvotes: 2