How to Include and Refer. .JS in my project?

2

Well it's the following, I have a .JS file in my project.

I'm calling it in HEAD SO:

    <script type ="text/javascript" src="~/JS/validacao.js"></script>

within this validation. JS has a masquerade function.

and put the following event in Textbox.

this.TxtCEP.Attributes.Add("onkeypress", "Mascara(CEP, TxtCEP);");

<asp:TextBox ID="TxtCEP" runat="server" style="margin-left: 66px" Width="231px" TextMode="Number" MaxLength ="8"></asp:TextBox>

Mascara Function

   function Mascara(formato, objeto) {
campo = eval (objeto);
// CEP
if (formato=='CEP'){
    var CodCar = event.keyCode;
    if (CodCar < 48 || CodCar > 57) {
        campo.focus();
        event.returnValue = false;
    }
    separador = '-'; 
    conjunto1 = 5;
    if (campo.value.length == conjunto1) {
        campo.value = campo.value + separador;
    }
  }
}

But it is not working.

Am I doing it right?

    
asked by anonymous 08.12.2017 / 13:04

1 answer

0

In web forms project, to add the reference to a script, you can do it as follows.

<script type ="text/javascript" src="Scripts/JS/validacao.js"></script>

However, I prefer to do the following mandeira

<script type ="text/javascript" src="<%=ResolveUrl("~/Scripts/JS/validacao.js")%>"></script>

So I guarantee that if I publish the project to a subfolder of the main domain, it will pick up the correct reference to my script

Another detail you're missing is when it's time to assign the event to your component, this is the correct way I did in the example I posted to my github

this.TxtCEP.Attributes.Add("onkeypress", $"Mascara('CEP', {this.TxtCEP.ClientID});");

The difference for yours is that I am passing the zip code as string and using ClientID that gets the identifier of the control generated by ASP. NET.

    
08.12.2017 / 14:37