最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Auto run Javascript on page load - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 2

jQuery.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!');
});
发布评论

评论列表(0)

  1. 暂无评论