jquery文档准备功能太长,我想导出它的一部分

jquery document ready function too long, I want to export parts of it

好的,虽然我已经使用这个网站很长时间了,但这是我第一次提问。我似乎找不到我要找的东西。

我有一个文档就绪功能,其中包含我希望 "export" 可以使用的部分,因此我不需要在每一页上重复它们。有点像页眉、页脚甚至主容器的 ASP 包含。这可行吗?请看我的意思:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Some Title</title>
<link rel="styleSheet" href="somePath/myCssFile.css" type="text/css" media="screen" />
<script src="../somePath/jquery.min.js" type="text/javascript"></script>
<script src="../somePath/my_own_JS_stuff.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
    // this part is unique to THIS page

    $('#myUniqueDiv').delay(1000).fadeIn(500);
    $(document).attr("title", "MisterKay.com | " + someArray[parseInt(a) + parseInt(b)]);
    $('#myUniqueDiv').hover(function(){ 
        // some code here
    }, function(){
        // some more code here
    });

    // This part is repeated on all pages of the site.
    // I wish I could take this part out of here, put it in a file and just call the file, like a virtual file in ASP.
    // That way, if I make a modification, I could do it once for the entire site, instead for doing it for each page.
    // I cannot put it in a separate JS file either; for some reason it gives me an error.
    // My guess is this happens because this is INSIDE of the document-ready-function.
    // But I also cannot put it outside of the document-ready-function. 
    $('#someRepeatedDiv').cycle({ 
        fx:     'fade', 
        speed:  1000, 
        timeout:    3000
    });
    $('#someRepeatedInputBox').blur(function(){
        var sVal = $(this).val();
        if(sVal == ""){
            $(this).val(someArray[a]);
            $(this).css('color','#999');
        }
    });
    // End of repeated section of the document-ready-function
}); // End of document-ready-function
</script>
</head>
<body>
        <!-- #include virtual="header.html" -->
        <!-- #include virtual="main.html" -->
        <!-- #include virtual="footer.html" -->
</body>
</html>

如何将我需要在每一页上重复的部分放入某个外部文件,并且仍然让文档就绪函数读取它?

再次感谢您的回答!

你可以试试jQuery $.getScript():

Load a JavaScript file from the server using a GET HTTP request, then execute it.

示例:

$.getScript( "ajax/test.js", function( data, textStatus, jqxhr ) {
  console.log( data ); // Data returned
  console.log( textStatus ); // Success
  console.log( jqxhr.status ); // 200
  console.log( "Load was performed." );
});

更多详情:http://api.jquery.com/jquery.getscript/