0

I have 4 javascripts within <script> tags in all multiple of my content pages. They handle popups and disabling of other buttons on button clicks. So at the moment I have a lot of duplicate script code on every page.

Is it possible to have these scripts in the master page instead and have all my content pages reference them? If so, where in the master page do I put them and how do I reference them on a button click?

I have searched google but haven't found any good answers.

Edit 1: To deal with the duplicate marking.

I am mostly looking to avoid duplicate code here, that is why I want to put this in my master page. If it is possible to put my scripts in a JS file, include that on the master page and have all my content pages access the scripts from there. Then that is absolutely a solution to my question and will gladly accept an answer that describes how I do that. To be clear, it is the accessing part that I haven't found how to do.

But just saying something like, "put the scripts in a JS file and include the file on your page", is not a solution to my question since it just hides the duplicity in files instead.

Edit 2: What I have tried now.

As per M Idrees's answer below I have put my scripts into buttonScripts.js, which is located in my projects Scripts folder. I added it to the head of my master page:

<head runat="server">
    <script type="text/javascript" src="~/Scripts/buttonScripts.js"></script>
    ...
</head>

I kept the click functions on my buttons as is:

<button ... onclick="if (!myFunc(this)) { return false; }"></button>

Then I removed the scripts from my content pages and started my web app. Now I get the error "myFunc is undefined". This is what I mean by "it is the accessing part that I haven't found how to do" above.

Edit 3: I suck.

Tried to use a relative path, as you can see above. With a proper path:

<head runat="server">
    <script type="text/javascript" src="Scripts/buttonScripts.js"></script>
    ...
</head>

It works as intended. Thanks!

Skillzore
  • 748
  • 7
  • 21
  • 1
    Take them out of ` – Nick is tired Aug 09 '17 at 09:10
  • Put your script in an external file, then use . – iquellis Aug 09 '17 at 09:14
  • @NickA Just because your solution was to put my script in a JS file does not make my question a duplicate. This question is open to that solution and others. – Skillzore Aug 09 '17 at 09:15
  • @iquellis Can I put that in my master page or do I still have to have duplicate code on all my content pages? – Skillzore Aug 09 '17 at 09:16
  • @Skillzore if you add it to your masterpage this will get added on every page you are using your masterpage on. Meaning if there are more then the 4 pages you mentioned. The other pages would get the JS file sended which would not be needed. – H.Mikhaeljan Aug 09 '17 at 09:28
  • @H.Mikhaeljan tbh I don't know why I wrote multiple. The scripts are used in all my pages atm. But I will consider if I will have pages that won't use these scripts in the future. – Skillzore Aug 09 '17 at 09:32

1 Answers1

1

Within the head tag of your master page. Add your script reference, like:

<script type="text/javascript" src="path/to/script.js"></script>

Remove all other script references from your content pages. It will automatically get from master page.

Regarding your point: it is the accessing part that I haven't found how to do.

You don't have to change anything about accessing script code. It will work as it might be working now.

Do these steps, and if still have any problem, just update in this post.

M_Idrees
  • 2,080
  • 2
  • 23
  • 53