10 Replies - 6063 Views - Last Post: 25 August 2010 - 01:09 PM Rate Topic: -----

#1 ariebsomer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-August 10

Calling JS function from MasterPage

Posted 23 August 2010 - 07:00 AM

I have an asp.net page that is calling a Javascript .js file from the JS folder in my root directory in the masterpage like so:

<script src="/js/report_home.js" type="text/javascript" language="javascript"></script>


It is inside the <head> of the masterpage.

For some reason, I can't get my javascript function in this file to work in my asp.net page. If I put the function directly into the <head> of the masterpage, it works just fine, leading me to believe that I'm missing something simple in my directory pathway or something.

I've tried getting rid of the leading '/', adding a '~', adding a './' in the beginning... but nothing is working.

Anyone have any idea?

Is This A Good Question/Topic? 0
  • +

Replies To: Calling JS function from MasterPage

#2 Frinavale   User is offline

  • D.I.C Addict
  • member icon

Reputation: 205
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Calling JS function from MasterPage

Posted 23 August 2010 - 09:00 AM

Interesting problem, I have always added links to external Javascript resources into the <head> section of the page. I've never tried adding a link to an external Javascript resource in the <body> of the page....

If it works in the <head> section, why not just leave it there?

-Frinny
Was This Post Helpful? 0
  • +
  • -

#3 ariebsomer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-August 10

Re: Calling JS function from MasterPage

Posted 23 August 2010 - 09:25 AM

The references are only in the <head> portion of the master page. The <body> sections only contain the contentplaceholder references.

I'm just trying to keep code clean here. I'm porting from a classic asp project, and really wanted to keep all of my javascript in individual files in a seperate 'js' folder and then just reference them from the master page.

So while directly putting the javascript into the master page does work, there is a LOT of js in this intranet site, so it adds a TON of code to the master page and then subsequently compiles it all at run-time, which isn't really necessary.

I also understand that I can create if statements to just call the javascript needed for each page, but I wanted to try to get it to just WORK with the files in the js folder first (like class files work in the App_Code folder).
Was This Post Helpful? 0
  • +
  • -

#4 Frinavale   User is offline

  • D.I.C Addict
  • member icon

Reputation: 205
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Calling JS function from MasterPage

Posted 23 August 2010 - 09:35 AM

Ok... it sounds like you have Javascript resources specific to particular pages in your website and that you want to add references to these resources dynamically when the page is requested.

Don't put this code in your MasterPage, put it in your individual pages.

If you are using the Ajax (UpdatePanels) then register your Javascript resources using the ScriptManager.

If you are not using Ajax, then register your Javascript resources using the Page.ClientScript property (that returns a reference to a ClientScriptManager class that you can use to register your Javascript resources with the page).

-Frinny
Was This Post Helpful? 1
  • +
  • -

#5 ariebsomer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-August 10

Re: Calling JS function from MasterPage

Posted 23 August 2010 - 11:38 AM

Well, that seems like a handy bit of info, so I thank you for that.

I tried my hand at implementing RegisterClientScriptBlock... this is the code I have in my masterpage (I'm just referencing the one javascript file for now, before I try others), but I'm still getting nothing to work unless the javascript functions themselves are directly coded into the master page - which I don't get (because it seems like everything is in order and it should be working).

Have I made a ridiculous error somewhere here?

Dim csName As [String] = "jsScript"
            Dim csType As Type = Me.[GetType]()

            ' Get a ClientScriptManager reference from the Page class.
            Dim cs As ClientScriptManager = Page.ClientScript


            If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
                Dim sText As String = "<script src='~/js/report_home.js' type='text/javascript' language='javascript'></script>"
                cs.RegisterClientScriptBlock(csType, csName, sText)
            End If


Was This Post Helpful? 0
  • +
  • -

#6 ariebsomer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-August 10

Re: Calling JS function from MasterPage

Posted 24 August 2010 - 05:53 AM

View Postariebsomer, on 23 August 2010 - 10:38 AM, said:

Well, that seems like a handy bit of info, so I thank you for that.

I tried my hand at implementing RegisterClientScriptBlock... this is the code I have in my masterpage (I'm just referencing the one javascript file for now, before I try others), but I'm still getting nothing to work unless the javascript functions themselves are directly coded into the master page - which I don't get (because it seems like everything is in order and it should be working).

Have I made a ridiculous error somewhere here?

Dim csName As [String] = "jsScript"
            Dim csType As Type = Me.[GetType]()

            ' Get a ClientScriptManager reference from the Page class.
            Dim cs As ClientScriptManager = Page.ClientScript


            If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
                Dim sText As String = "<script src='~/js/report_home.js' type='text/javascript' language='javascript'></script>"
                cs.RegisterClientScriptBlock(csType, csName, sText)
            End If



So far no solution except to put js code directly into aspx files... is there any kind of IIS setting or anything that could be preventing this from working when the files are in another folder? I am guessing not, since JS is client-side, but I have tried every syntax known to man, and this should be working.
Was This Post Helpful? 0
  • +
  • -

#7 Frinavale   User is offline

  • D.I.C Addict
  • member icon

Reputation: 205
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Calling JS function from MasterPage

Posted 24 August 2010 - 11:09 AM

I think you were just using the wrong method to register the Javascript.
Try using the ClientScriptManager.RegisterClientScriptInclude method or the ClientScriptManager.RegisterStartupScript method.

:)

-Frinny
Was This Post Helpful? 1
  • +
  • -

#8 ariebsomer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-August 10

Re: Calling JS function from MasterPage

Posted 25 August 2010 - 12:07 PM

Well, I finally got it to work - on the logic side or the presentation side. Frinny, you're awesome!

ClientScript.RegisterStartupScript(Me.[GetType](), "MyScript", "<script language=javascript src='report_home.js'></script>")


For some reason, it just didn't like the fact that I had my files in another folder - when I put them in the root, it worked just fine. I have no idea why this subfolder prevented access, but hey....

Thanks.
Was This Post Helpful? 0
  • +
  • -

#9 Frinavale   User is offline

  • D.I.C Addict
  • member icon

Reputation: 205
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Calling JS function from MasterPage

Posted 25 August 2010 - 12:58 PM

I'm glad you got this to work :)
Was This Post Helpful? 0
  • +
  • -

#10 ariebsomer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-August 10

Re: Calling JS function from MasterPage

Posted 25 August 2010 - 12:59 PM

I do have one more question, if someone doesn't mind though...

All of my regular aspx.vb pages that have the ClientScriptBlockRegistered command work just fine. But I have a couple of class files - files that only contain functions or subs - that I didn't make actual 'pages' because they don't ever really exist (they were just used in <!--includes--> statements in the classic asp).

So in those files (which obviously don't have a page_load event), I put an if statement in each sub so that no matter what sub got called (this is a toolbar with multiple buttons powered by js), it would check to see if the script had been registered yet, and if not, then register it.

But these don't work. Am I fundamentally approaching this the wrong way, or doing something stupid (or are they one and the same? lol) Or is this related to the fact that the class files sit in the App_Code subdirectory? (the sub folder seemed to be the problem originally)

'javascript file call
            If (Not ClientScript.IsStartupScriptRegistered("JSScript")) Then
                ClientScript.RegisterStartupScript(Me.[GetType](), "MyScript", "<script language=javascript src='report_home.js'></script>")
            End If

Was This Post Helpful? 0
  • +
  • -

#11 Frinavale   User is offline

  • D.I.C Addict
  • member icon

Reputation: 205
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Calling JS function from MasterPage

Posted 25 August 2010 - 01:09 PM

Where is this code?
In a Class?

Registering the Javascript with the page really should be the responsibility of the page or the control that requires the Javascript....

I don't fully understand your application though.
From the sounds of it, you have Javascript driven menu but I'm not sure if this menu is in a user control, a page, in a class, spread-out across multiple pages/code chunks ...

If you elaborate on what you're feature is supposed to do I might be able to help you further (you really should ask new questions in a new thread though...just forum etiquette)

-Frinny
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1