Embed the javascript file.

J

Jianwei Sun

Hi, Dear guru,

I have a question on embed a javascript into the master page.

I put the following line
<link runat="server" type="text/javascript" href="~/column.js" />

under the head, but obviously, it's not called anywhere.


But if I put this line,
<script type="text/javascript" src="column.js"></script>, it worked.

But I think i have to use the first approach since the second approach
will use an absolute path, and won't resolve in other pages which
include the master page.

Is there anyway around this issue

Thanks for help.
 
S

Swanand Mokashi

Here is how you can do it

define the <head> tag in the master page to be a server side control as
this :
<head runat="server">

Then in Page_Init event of master page add the following :
HtmlLink link = new HtmlLink();
link.Href = "~/column.js";
link.Attributes.Add("type", "text/javascript");
Page.Header.Controls.Add(link);

HTH !
--
Swanand Mokashi
Microsoft Certified Solution Developer (.NET) - Early Achiever
Microsoft Certified Application Developer (.NET)

http://www.dotnetgenerics.com/
DotNetGenerics.com -- anything and everything about Microsoft .NET
technology ...

http://www.swanandmokashi.com/
http://www.swanandmokashi.com/HomePage/WebServices/
Home of the Stock Quotes, Quote of the day and Horoscope web services
 
J

Jianwei Sun

Thanks for the reply,

Here is what is generated in header section after implementing your
suggestions.
<link href="column.js" type="text/javascript" />

But it seems it's still not called, is that because my javascript is wrong.

[Javascript code ]

function setTall() {
if (document.getElementById) {
//Adjust the column height here....
}
}

window.onload = function() {
setTall();
}

window.onresize = function() {
setTall();
}

But if I uncomment this line, it works...

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


Any other suggestions, thanks.
 
Joined
Mar 26, 2008
Messages
1
Reaction score
0
embed external javascript in master page head

The HTML LINK tag can't be used for Javascript. You have to use the SCRIPT tag for embedding external Javascript. You can use the ResolveClientUrl function to translate the relative URL, and a Literal control to print the SCRIPT tag.

Put this in the Page_Load of your master page:

ExternalJS.Text = "<script type=\"text/javascript\" src=\"";
ExternalJS.Text += ResolveClientUrl("~/column.js");
ExternalJS.Text += "\"></script>";


Put this in your <head> tag of your master page:

<asp:literal id="ExternalJS" runat="server"></asp:literal>
 
Joined
May 21, 2008
Messages
1
Reaction score
0
humbads said:
The HTML LINK tag can't be used for Javascript. You have to use the SCRIPT tag for embedding external Javascript. You can use the ResolveClientUrl function to translate the relative URL, and a Literal control to print the SCRIPT tag.

Put this in the Page_Load of your master page:

ExternalJS.Text = "<script type=\"text/javascript\" src=\"";
ExternalJS.Text += ResolveClientUrl("~/column.js");
ExternalJS.Text += "\"></script>";


Put this in your <head> tag of your master page:

<asp:literal id="ExternalJS" runat="server"></asp:literal>


Works like a charm, thanks Humbads!

note: in VB.net the escape characters for the double quotes is simply two double quotes, so your literal looks like
litJSLink.Text = "<script type=""text/javascript"" src=""" & ResolveClientUrl("~/javscriptFile.js") & """></script>"
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top