I'm trying to run a piece of JavaScript automatically when a page loads. The code itself simply pops up a link to our self service portal...
// Requires jQuery!
jQuery.ajax({
url: "selfserviceportal",
type: "get",
cache: true,
dataType: "script"
});
I've attempted using onload and onclick with no success. Any help much appreciated.
I'm trying to run a piece of JavaScript automatically when a page loads. The code itself simply pops up a link to our self service portal...
// Requires jQuery!
jQuery.ajax({
url: "selfserviceportal.",
type: "get",
cache: true,
dataType: "script"
});
I've attempted using onload and onclick with no success. Any help much appreciated.
Share Improve this question edited Feb 20, 2014 at 12:03 Trufa 40.8k44 gold badges132 silver badges193 bronze badges asked Jan 21, 2013 at 13:23 user1997178user1997178 11 silver badge1 bronze badge 4- how did you try to use onload?? – philipp Commented Jan 21, 2013 at 13:25
- 1 I removed the java tag. You should know that Java and JavaScript are two entirely different things! – Veger Commented Jan 21, 2013 at 13:25
- add an 'success' handler to get a feedback when the request finishes – philipp Commented Jan 21, 2013 at 13:26
- What is happening in your case? is the ajax request going? what is the response of the request? – Arun P Johny Commented Jan 21, 2013 at 13:28
3 Answers
Reset to default 2jQuery.ready() Specifies a function to execute when the DOM is fully loaded.
$(document).ready(function () {
// your code
}
Just include your JS file at the end of the document and execute your code.
Read more on my relevant blog post You Don't Need the DOM Ready event
$(function() {
console.log('Page loaded!');
});