开发者

Jquery onclick event to change to onload

开发者 https://www.devze.com 2023-04-01 20:19 出处:网络
I am using following code which works perfeet when i click onLink1, what i want to do is it should automatically on pageload fire the event loadContent, how to do that ?

I am using following code which works perfeet when i click on Link1, what i want to do is it should automatically on pageload fire the event loadContent, how to do that ?

<head>
<title>jQuery开发者_Go百科 test page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
function loadContent(elementSelector, sourceUrl) {
$(""+elementSelector+"").load("http://abc.com/"+sourceUrl+"");
}
  </script>

</head>
<body>
<a href="javascript:loadContent('#content', 'displayimages.php');">Link 1</a>
<div id="content">content will be loaded here</div>
</body>
</html>


Just call the method on page load

$(document).ready(function(){
   loadContent('#content', 'displayimages.php');
});


Please this in a script tag in your document. The code contained within this block will be executed when the page has fully loaded.

$( function() {
    loadContent('#content', 'displayimages.php');
}


Just add loadContent('#content', 'displayimages.php') outside the function within the script tag, so it's called as soon as the page loads. Basically, your script tag will now look like

    <script type="text/javascript">  
// The function call
    loadContent('#content', 'displayimages.php')

// The actual function
    function loadContent(elementSelector, sourceUrl) {  
    $(""+elementSelector+"").load("http://abc.com/"+sourceUrl+"");  
    }  
    </script>  

Another option would be to call it on body onload, by changing your body tag to

<body onload="loadContent('#content', 'displayimages.php')">.

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消