I’ve started working on a new site, and along the way, I’ve built up a small collection of JavaScript functions that I use on various pages. So what I thought I’d do is collect up
all the functions and add them into one JavaScript page which I then link into my master page.
What I’ve found is that for content pages using the master page within the same folder (the root in this case) no problem, but as soon as I use a the master page with a content page
on a different folder level, the JavaScript does not get included.
Thanks, but I tried that to no effect. I think that only works with server controls, and as this is a simple link script in the head, it won't work????
you can refer javascript with relative to root path for eg. src="/javascript/jscript.js" make sure javascript folder recides in the root folder otherwise adjust the path according..... Regards Sreeju
The real voyage of discovery consists not in making new landscapes, but in having new eyes
Thanks for the help, although the changes suggested will only work for the pages related to the given path directory; as soon as the master page with the JS link is used on a page
that’s on a different level, the JS link is no longer valid.
What I think I need is a server control to like the JS file, then I’ll be able to use the ~/
i think you might be using the java script some thing like this.if you have getElementByid(''TextBox1").value, when you use this java script with the Masterpages it will not accept because when you use the Masterpages the control id ("textBox1") will be
generated automatically,. on the client side the id that was generated for the TextBox1 is some thing like this "ctl00_ContentPlaceHolder1_TextBox1. so to avoid this in your java script, use some thing as shown below using the
ClientID. when you use the ClientID it will take the autogenerated identifier for the control. For sure this will not effect the other pages that you already has which doesnt use the java script.
document.getElementById(
'<%=TextBox1.ClientID%>')
Let me know if you have any other question..
---------------------------------------------
Please Mark Post that helped you as answer
document.getElementById('<%=TextBox1.ClientID%>'). This will give us access to
TextBox1. Now, this will work fine until and unless the script is inline with the ASPX page, i.e., if the script is included as part of the ASPX page.
But, the same won’t work if you have the script in a separate .js file and add it to the ASPX page by specifying its location.
for that,
write the javascript class as follows in seperate .js page;
RichardLaw
Member
464 Points
626 Posts
JavaScript Includes not working in Master Pages
Jul 07, 2007 09:48 PM|LINK
Hi
I’ve started working on a new site, and along the way, I’ve built up a small collection of JavaScript functions that I use on various pages. So what I thought I’d do is collect up all the functions and add them into one JavaScript page which I then link into my master page.
<script src="javaScript/JScript.js" type="text/javascript"></script>
What I’ve found is that for content pages using the master page within the same folder (the root in this case) no problem, but as soon as I use a the master page with a content page on a different folder level, the JavaScript does not get included.
How can I fix this?
Many thanks as always
Richard
nomercy007
Participant
1561 Points
297 Posts
Re: JavaScript Includes not working in Master Pages
Jul 08, 2007 06:20 AM|LINK
you need to change make the the path javascript file to include "~" which refers to the root directory. Make it :
src="~javaScript/JScript.js"
in your case, it will refer to the same folder as the content page.
RichardLaw
Member
464 Points
626 Posts
Re: JavaScript Includes not working in Master Pages
Jul 08, 2007 07:05 AM|LINK
Hi
Thanks, but I tried that to no effect. I think that only works with server controls, and as this is a simple link script in the head, it won't work????
nomercy007
Participant
1561 Points
297 Posts
Re: JavaScript Includes not working in Master Pages
Jul 08, 2007 08:13 AM|LINK
Hi,
try "../"
I think it will work
src="../javaScript/JScript.js"
good luck,
sreejukg
All-Star
27557 Points
4111 Posts
Re: JavaScript Includes not working in Master Pages
Jul 08, 2007 08:28 AM|LINK
My Blog
RichardLaw
Member
464 Points
626 Posts
Re: JavaScript Includes not working in Master Pages
Jul 08, 2007 08:34 AM|LINK
Thanks for the help, although the changes suggested will only work for the pages related to the given path directory; as soon as the master page with the JS link is used on a page that’s on a different level, the JS link is no longer valid.
What I think I need is a server control to like the JS file, then I’ll be able to use the ~/
Thanks
Richard
RichardLaw
Member
464 Points
626 Posts
Re: JavaScript Includes not working in Master Pages
Jul 08, 2007 09:11 AM|LINK
I've tried the following:
protected void Page_Init(object sender, EventArgs e){
HtmlLink link = new HtmlLink();link.scr =
"~/javaScript/JScript.js"; link.Attributes.Add("type", "text/javascript");Page.Header.Controls.Add(link);
}
The source file reference works great, but a link to the file doesn't work - needs to be in script tags... Any thoughts???
Thanks
Richard
sreejukg
All-Star
27557 Points
4111 Posts
Re: JavaScript Includes not working in Master Pages
Jul 08, 2007 09:15 AM|LINK
src="/javascript/jscript.js"
My Blog
mythu
Member
136 Points
54 Posts
Re: JavaScript Includes not working in Master Pages
Jul 08, 2007 10:15 AM|LINK
RichardLaW,
i think you might be using the java script some thing like this.if you have getElementByid(''TextBox1").value, when you use this java script with the Masterpages it will not accept because when you use the Masterpages the control id ("textBox1") will be generated automatically,. on the client side the id that was generated for the TextBox1 is some thing like this "ctl00_ContentPlaceHolder1_TextBox1. so to avoid this in your java script, use some thing as shown below using the ClientID. when you use the ClientID it will take the autogenerated identifier for the control. For sure this will not effect the other pages that you already has which doesnt use the java script.
document.getElementById(
'<%=TextBox1.ClientID%>')Let me know if you have any other question..
Please Mark Post that helped you as answer
chamika
Member
6 Points
3 Posts
Re: JavaScript Includes not working in Master Pages
Feb 19, 2010 07:54 AM|LINK
Try this,
document.getElementById('<%=TextBox1.ClientID%>'). This will give us access to TextBox1. Now, this will work fine until and unless the script is inline with the ASPX page, i.e., if the script is included as part of the ASPX page.
But, the same won’t work if you have the script in a separate .js file and add it to the ASPX page by specifying its location.
for that,
write the javascript class as follows in seperate .js page;
(){();}
call this in any event as follows:
<asp:TextBox ID="txtMyValText" CssClass="search" runat="server" Text="Display Message" onblur="myjavascript( this.id);"></asp:TextBox>
Try this links as well :
http://www.codeproject.com/KB/scripting/Masterpage-Javascript.aspx