views:

84

answers:

4

I am trying to call some javascript functions which I ahve written in some file. Eg.

function OpenPopup() {

    alert("OpenPopUp");
        return false;

}

when I call this from button from OnClientClick = "OpenPopup()" it is not called but when I put this function on MasterPage it is able to call the function.

I added this is the MasterPages's Head

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

Please let me know what can be the possiblities why it is not called.

A: 

There should be a ContentPlaceHolder in the pasterpage, in the head section with an id 'head'. You should place a Content tag in the slave page referring to that 'head' section place holder and place this script reference inside that. This way, the < script > tag is placed inside the < head > tag.

Gorkem Pacaci
I used it like <head><script src="Scripts/something.js" type="text/javascript"></script> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder></head> so it should not be a problem
Nits
A: 

You might have given wrong scr for script file.just drag the file path in your master page.Doing so you will get the accurate path for file.

Amit
An easy way to debug this is to open the page in Chrome, right click and select Inspect Element. Open the resource tracker and highlight the JS file. You can see the URI it is attempting to get the JS file from, the return code as well as the content itself.
Krisc
A: 

From what I gather from your question, your function call now works with the <script> tag in place, and you are unsure why the tag is necessary.

If you write your javascript function in a file, say "script.js" and then attempt to call the function in an ASP.NET page, the ASP.NET is not aware that script.js exists.

The <script> tag is letting your ASP.NET page know to include the file script.js so that any functions and variables defined in that file can be used. By putting the <script> tag in your MasterPage, you are including it in every page on your site. A better idea might be to include the <script> tag only on the pages in which you intend to use the javascript functions.

Kevin
A: 

I tried using this and it worked

I don't know why but I checked with putting the absolute path instead of relative path and it worked. Thing is I chnaged it to While refrencing to the CSS is working fine with that relative path. I have no clue why it is happening

Nits