你如何在MasterPage中包含JavaScript?

 杰ZGJ8513 发布于 2023-02-09 18:43

我在VS 2012,.NET framework 4.5中使用C#构建ASP.NET Webform应用程序

我在应用程序的根目录中有一个MasterPage,JavaScript文件在名为js的文件夹中.

这是问题:如果页面在根文件夹中,那么一切正常(css + js),如果我在子文件夹中放置任何页面然后css工作但是那些JavaScripts 根本不起作用,显然参考路径是错误的.

所以Css引用路径很好,但对于脚本而言,无论我使用什么,它们都不起作用(js/script.js或〜/ js/script.js或/js/script.js或../ ResolveUrl,ResolClientveUrl ......或者这一切方法http://yobriefca.se/blog/2010/10/19/%3C-head-%3Eache-including-javascript-in-asp-dot-net-master-pages/)它们都引用root/SUB-FOLDER/js/script.js而不是root/js/script.js

在root中:单个MasterPage,Default.aspx,test.aspx,js文件夹,css文件夹和Pages文件夹.默认和测试页面是工作文件,但Pages文件夹中的所有页面都不显示.js如果页面不在根目录中,那么路径是错误的

在我的母版页面中:






<%-- tried these and lot more but NOT workkkkkkkkkkk --%>

<%--
--%>

<%--
--%>

<%--
--%>



script.js是这样的:

....
    $.include('js/superfish.js')
$.include('js/FF-cash.js')
$.include('js/tms-0.4.x.js')
$.include('js/uCarausel.js')
$.include('js/jquery.easing.1.3.js')
$.include('js/jquery.tools.min.js')
$.include('js/jquery.jqtransform.js')
$.include('js/jquery.quicksand.js')
$.include('js/jquery.snippet.min.js')
$.include('js/jquery-ui-1.8.17.custom.min.js')
$.include('js/jquery.cycle.all.min.js')
$.include('js/jquery.cookie.js')
$(function(){
    if($('.tweet').length)$.include('js/jquery.tweet.js');
    if($('.lightbox-image').length)$.include('js/jquery.prettyPhoto.js');
    if($('#contact-form').length||$('#contact-form2').length)$.include('js/forms.js');
    if($('.kwicks').length)$.include('js/kwicks-1.5.1.pack.js');
    if($('#counter').length)$.include('js/jquery.countdown.js');
    if($('.fixedtip').length||$('.clicktip').length||$('.normaltip').length)$.include('js/jquery.atooltip.pack.js')
// Slider
    $('.main-slider')._TMS({
.....

Web浏览器的开发人员工具(控制台)中的错误:

    Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/tms-0.4.x.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/uCarausel.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/jquery.jqtransform.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/jquery.quicksand.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/jquery.snippet.min.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/FF-cash.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/superfish.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/jquery.tools.min.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/jquery-ui-1.8.17.custom.min.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/jquery.cycle.all.min.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/jquery.easing.1.3.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/jquery.cookie.js
Uncaught TypeError: Object [object Object] has no method '_TMS' script.js:22
event.returnValue is deprecated. Please use the standard event.preventDefault() instead.

Code Maveric.. 16

HTML

除了Modernizr等具有特征检测功能的脚本之外,您通常不需要任何脚本.将所有脚本移动到页面底部是一种最佳实践,如下所示:



    
    
    



    

    
    





的script.js

引用script.js中的其他脚本文件将需要将/其添加到'js /`,如下所示:

$.include('/js/superfish.js');
$.include('/js/FF-cash.js');
$.include('/js/tms-0.4.x.js');
$.include('/js/uCarausel.js');
$.include('/js/jquery.easing.1.3.js');
$.include('/js/jquery.tools.min.js');
$.include('/js/jquery.jqtransform.js');
$.include('/js/jquery.quicksand.js');
$.include('/js/jquery.snippet.min.js');
$.include('/js/jquery-ui-1.8.17.custom.min.js');
$.include('/js/jquery.cycle.all.min.js');
$.include('/js/jquery.cookie.js');

if($('.tweet').length)
    $.include('/js/jquery.tweet.js');

if($('.lightbox-image').length)
    $.include('/js/jquery.prettyPhoto.js');

if($('#contact-form').length || $('#contact-form2').length)
    $.include('/js/forms.js');

if($('.kwicks').length)
    $.include('/js/kwicks-1.5.1.pack.js');

if($('#counter').length)
    $.include('/js/jquery.countdown.js');

if($('.fixedtip').length || $('.clicktip').length || $('.normaltip').length)
    $.include('/js/jquery.atooltip.pack.js');

// Slider
$('.main-slider')._TMS({



MISC

在测试所有这些内容时,不要忘记清除缓存或进行隐私浏览!

1 个回答
  • HTML

    <head />除了Modernizr等具有特征检测功能的脚本之外,您通常不需要任何脚本.将所有脚本移动到页面底部是一种最佳实践,如下所示:

    <html>
    <head runat="server">
        <title></title>
        <link rel="stylesheet" href='<%= ResolveUrl("~/css/style.css") %>' />
        <asp:ContentPlaceHolder ID="Head" runat="server" />
    </head>
    <body>
    
        <!-- Scripts at bottom of page for faster loading. -->
    
        <script src='<%= ResolveUrl("~/js/jquery-1.7.1.min.js") %>'></script>
        <script src='<%= ResolveUrl("~/js/script.js") %>'></script>
    
    </body>
    </html>
    



    的script.js

    引用script.js中的其他脚本文件将需要将/其添加到'js /`,如下所示:

    $.include('/js/superfish.js');
    $.include('/js/FF-cash.js');
    $.include('/js/tms-0.4.x.js');
    $.include('/js/uCarausel.js');
    $.include('/js/jquery.easing.1.3.js');
    $.include('/js/jquery.tools.min.js');
    $.include('/js/jquery.jqtransform.js');
    $.include('/js/jquery.quicksand.js');
    $.include('/js/jquery.snippet.min.js');
    $.include('/js/jquery-ui-1.8.17.custom.min.js');
    $.include('/js/jquery.cycle.all.min.js');
    $.include('/js/jquery.cookie.js');
    
    if($('.tweet').length)
        $.include('/js/jquery.tweet.js');
    
    if($('.lightbox-image').length)
        $.include('/js/jquery.prettyPhoto.js');
    
    if($('#contact-form').length || $('#contact-form2').length)
        $.include('/js/forms.js');
    
    if($('.kwicks').length)
        $.include('/js/kwicks-1.5.1.pack.js');
    
    if($('#counter').length)
        $.include('/js/jquery.countdown.js');
    
    if($('.fixedtip').length || $('.clicktip').length || $('.normaltip').length)
        $.include('/js/jquery.atooltip.pack.js');
    
    // Slider
    $('.main-slider')._TMS({
    



    MISC

    在测试所有这些内容时,不要忘记清除缓存或进行隐私浏览!

    2023-02-09 18:45 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有