vishali
vishali

Reputation: 95

How to make javascript file work in aspx?

I have javascript functions for shortcut keys so want to use in entire solution so i want that file in a javascript file use it in every screen so tell me how to use it. here is my code.

javascript

<script type="text/javascript">
        var isCtrl = false;
        document.onkeydown = function (e) {
            // alert(event.keyCode);
            if (event.keyCode == 17) isCtrl = true;
            if (event.keyCode == 67 && isCtrl == true) {
                document.getElementById('<%= btnCreate.ClientID %>').click();
                //alert("Create New");
                //return false;
                exit();
            }
            if (event.keyCode == 83 && isCtrl == true) {
                document.getElementById('<%= btnSave.ClientID %>').click();
                //alert("Save");
                exit();
            }
            if (event.keyCode == 86 && isCtrl == true) {
                document.getElementById('<%= btnView.ClientID %>').click();
                //alert("View");
                exit();
            }
            if (event.keyCode == 82 && isCtrl == true) {
                document.getElementById('<%= btnRefresh.ClientID %>').click();
                //alert("Refresh");
                return false;
            }
            if (event.keyCode == 77 && isCtrl == true) {
                document.getElementById('<%= btnShow.ClientID %>').click();
                //alert("Show");
                exit();
            }
            if (event.keyCode == 68 && isCtrl == true) {
                document.getElementById('<%= btnDelete.ClientID %>').click();
                //alert("Delete");
                exit();
            }
        }
    </script>

tell me how to use this with a example.

Upvotes: 0

Views: 158

Answers (3)

kashyapa
kashyapa

Reputation: 1626

this is more of a architectural thing than anything else. If you are on ASP.NET technology i.e. be it WebForms or MVC - they all provide a concept of Master Page. As the name goes its a master view for all the other views. You can create other views by basing on the master view. The master view we usually create with the common header for the app and common footer or anything else which can be common for the site.

In your case, if you need some common javascript in all other views - the best logical place is to include the javascript in the master page. with this all other views which are based on master page will get the script automatically. use the script to include the JavaScript.

hope this helps...

Lohith (Tech Evangelist, Telerik India)

Upvotes: 1

Krishna Thota
Krishna Thota

Reputation: 7026

Use

<script type="text/javascript" src="Path/YourFileName.js"></script>

In Head Tag and you can use the functions in the JavaScript file directly in your Page

See this - Moving Scripts to File

There are some Best Practices for speeding up your website like - Placing the JavaScript code at the bottom of Body

Upvotes: 1

Mohit Pandey
Mohit Pandey

Reputation: 3813

Attach this file to head tag.

<head>
<script type="text/javascript"  src="your file name"></script>
</head>

Upvotes: 0

Related Questions