Search code examples
jqueryasp.netmaster-pages

using jquery in masterpages


hello dear fellow programmers i'm having a silly trouble using jquery in master pages which is i desire to call a function in masterpage itself not the related pages to the master page. here is the head of my masterpage

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="CMS_master.master.cs" Inherits="CMS_master" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
    <link href="css/Exclusive_fonts.css" rel="stylesheet" />
    <link href="Content/bootstrap.min.css" rel="stylesheet" />
    <link href="css/CMS_Style.css" rel="stylesheet" />
    <script src="<%ResolveUrl("~/Scripts/jquery-3.1.1.min.js"); %>" type="text/javascript">
                   window.alert("a");
    </script>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>

the setback is that jquery and javascripts codes are not recognized and rendered. i know there are questions that might have answered this problem but none of them worked for me.


Solution

  • You made 2 typos in the script tag. You forgot the = sign and remove the ; at the end.

    <script src="<%= ResolveUrl("~/Scripts/jquery-3.1.1.min.js") %>" type="text/javascript"></script>
    

    This renders in html as src="/Scripts/jquery-3.1.1.min.js"