How to add jQuery in JS file

后端 未结 19 642
星月不相逢
星月不相逢 2020-12-04 05:47

I have some code specific to sorting tables. Since the code is common in most pages I want to make a JS file which will have the code and all the pages using it can referen

相关标签:
19条回答
  • 2020-12-04 05:57

    it is not possible to import js file inside another js file

    The way to use jquery inside js is

    import the js in the html or whatever view page you are using inside which you are going to include the js file

    view.html

     <script src="<%=request.getContextPath()%>/js/jquery-1.11.3.js"></script>
     <script src="<%=request.getContextPath()%>/js/default.js"></script>
    

    default.js

    $('document').ready(function() {
        $('li#user').click(function() {
             $(this).addClass('selectedEmp');
        });
    });
    

    this will definitely work for you

    0 讨论(0)
  • 2020-12-04 05:58
    var script = document.createElement('script');
    script.src = 'https://code.jquery.com/jquery-3.4.1.min.js';
    script.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(script);
    
    0 讨论(0)
  • 2020-12-04 06:00

    If document.write('<\script ...') isn't working, try document.createElement('script')...

    Other than that, you should be worried about the type of website you're making - do you really think its a good idea to include .js files from .js files?

    0 讨论(0)
  • 2020-12-04 06:01

    If you want to include jQuery code from another JS file, this should do the trick:

    I had the following in my HTML file:

    <script src="jquery-1.6.1.js"></script>
    <script src="my_jquery.js"></script>
    

    I created a separate my_jquery.js file with the following:

    $(document).ready(function() {
      $('a').click(function(event) {
        event.preventDefault();
        $(this).hide("slow");
      });
    });
    
    0 讨论(0)
  • 2020-12-04 06:04

    I find that the best way is to use this...

    **<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>**
    

    This is from the Codecademy 'Make an Interactive Website' project.

    0 讨论(0)
  • You can create a master page base without included js and jquery files. Put a content place holder in master page base in head section, then create a nested master page that inherits from this master page base. Now put your includes in a asp:content in nested master page, finally create a content page from this nested master page

    Example:

    //in master page base    
    
    <%@  master language="C#" autoeventwireup="true" inherits="MasterPage" codebehind="MasterPage.master.cs" %>
    <html>
    <head id="Head1" runat="server">
        <asp:ContentPlaceHolder runat="server" ID="cphChildHead">
            <!-- Nested Master Page include Codes will sit Here -->
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
        <!-- some code here -->
    </body>
    </html>
    
    //in nested master page :
    <%@  master language="C#" masterpagefile="~/MasterPage.master" autoeventwireup="true"
        codebehind="MasterPageLib.master.cs" inherits="sampleNameSpace" %>
    <asp:Content ID="headcontent" ContentPlaceHolderID="cphChildHead" runat="server">
        <!-- includes will set here a nested master page -->
        <link href="../CSS/pwt-datepicker.css" rel="stylesheet" type="text/css" />
    
        <script src="../js/jquery-1.9.0.min.js" type="text/javascript"></script>
    
        <!-- other includes ;) -->
    </asp:Content>
    <asp:Content ID="bodyContent" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <asp:ContentPlaceHolder ID="cphChildBody" runat="server" EnableViewState="true">
            <!-- Content page code will sit Here -->
        </asp:ContentPlaceHolder>
    </asp:Content>
    
    0 讨论(0)
提交回复
热议问题