Embed the javascript file.

  • Thread starter Thread starter Jianwei Sun
  • Start date Start date
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.
 
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
 
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.
 

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

Back
Top