It's trying to compile my JavaScript?

G

Guest

I use Master Pages, so I make use of URL rebasing through the ~ operator,
like this in the <head>:
<link runat="server" href="~/Root.master.css" media="screen"
rel="stylesheet" type="text/css" />

It works perfectly and the ~ gets converted by the server into the correct
relative path. Great!

Now, I put this in the <head>:
<script type="text/javascript" src="~/Root.master.js"
runat="server"></script>

And everything screws up. It looks like it's trying to compile that .js file
into the page as the CLR throws up syntax errors and compiler errors in my
JavaScript (which is valid JavaScript).

What am I doing wrong? :(
 
I

Islamegy®

Why you try to make it harder...
There is more simple solutions
1- add html <Base> tag after head..
2- make <head runat="server"> then u can access it from code and add
anything you want here is example

foreach (Control ctrl in this.Master.Controls)
{
if (ctrl is HtmlHead)
{
HtmlTitle title = new HtmlTitle();
title.Text = Convert.ToString(hdt.Rows[0]["Title"]);

Literal baseurl = new Literal();
baseurl.Text = string.Format("<base href=\"{0}\">",
ConfigurationManager.AppSettings.Get("BaseURL"));


HtmlMeta desc = new HtmlMeta();
desc.Name = "Description";
desc.Content = Convert.ToString(hdt.Rows[0]["Description"]);

HtmlMeta keyword = new HtmlMeta();
keyword.Name = "Keyword";
keyword.Content = Convert.ToString(hdt.Rows[0]["Keyword"]);

Literal additional = new Literal();
additional.Text =
Convert.ToString(hdt.Rows[0]["AdditinalHeadTags"]);

HtmlHead head = (HtmlHead)ctrl;
head.Controls.AddAt(0, title);
head.Controls.AddAt(1, baseurl);
head.Controls.AddAt(2, desc);
head.Controls.AddAt(3, keyword);
head.Controls.AddAt(4, additional);
}
 
G

Guest

Hi Islamegy,

1) I'm not sure this will work for me. Doesn't the <base> tag need an
absolute URL? Even if it takes a relative URL, I'll still have to do
something to update the 'href' attribute on the fly to make it point to the
right level, such as:
<base href="~/" runat="server" />
(which doesn't work - I tried it - but ASP.NET doesn't parse the ~)

2) I'm using code-behind solutions for adding dynamic meta-tags, but that's
because they're dynamic. My JavaScript file is fixed, so I'd much prefer the
reference in the markup and not in the code-behind :)

Thanks...

Islamegy® said:
Why you try to make it harder...
There is more simple solutions
1- add html <Base> tag after head..
2- make <head runat="server"> then u can access it from code and add
anything you want here is example

foreach (Control ctrl in this.Master.Controls)
{
if (ctrl is HtmlHead)
{
HtmlTitle title = new HtmlTitle();
title.Text = Convert.ToString(hdt.Rows[0]["Title"]);

Literal baseurl = new Literal();
baseurl.Text = string.Format("<base href=\"{0}\">",
ConfigurationManager.AppSettings.Get("BaseURL"));


HtmlMeta desc = new HtmlMeta();
desc.Name = "Description";
desc.Content = Convert.ToString(hdt.Rows[0]["Description"]);

HtmlMeta keyword = new HtmlMeta();
keyword.Name = "Keyword";
keyword.Content = Convert.ToString(hdt.Rows[0]["Keyword"]);

Literal additional = new Literal();
additional.Text =
Convert.ToString(hdt.Rows[0]["AdditinalHeadTags"]);

HtmlHead head = (HtmlHead)ctrl;
head.Controls.AddAt(0, title);
head.Controls.AddAt(1, baseurl);
head.Controls.AddAt(2, desc);
head.Controls.AddAt(3, keyword);
head.Controls.AddAt(4, additional);
}



Boris Yeltsin said:
I use Master Pages, so I make use of URL rebasing through the ~ operator,
like this in the <head>:
<link runat="server" href="~/Root.master.css" media="screen"
rel="stylesheet" type="text/css" />

It works perfectly and the ~ gets converted by the server into the correct
relative path. Great!

Now, I put this in the <head>:
<script type="text/javascript" src="~/Root.master.js"
runat="server"></script>

And everything screws up. It looks like it's trying to compile that .js
file
into the page as the CLR throws up syntax errors and compiler errors in my
JavaScript (which is valid JavaScript).

What am I doing wrong? :(
 
I

Islamegy®

i don't know why u don't add it dynamicly like you do in meta tags, so u
don't have to change it the masterpage or forms, u can store it fixed in
web.config and add it from code behind.. easy huh?

you masy also right it like this
<script type="text/javascript" src="/scripts/master.js">
when use just "/" it's mean relative path from the root..

That's all i have..




Boris Yeltsin said:
Hi Islamegy,

1) I'm not sure this will work for me. Doesn't the <base> tag need an
absolute URL? Even if it takes a relative URL, I'll still have to do
something to update the 'href' attribute on the fly to make it point to
the
right level, such as:
<base href="~/" runat="server" />
(which doesn't work - I tried it - but ASP.NET doesn't parse the ~)

2) I'm using code-behind solutions for adding dynamic meta-tags, but
that's
because they're dynamic. My JavaScript file is fixed, so I'd much prefer
the
reference in the markup and not in the code-behind :)

Thanks...

Islamegy® said:
Why you try to make it harder...
There is more simple solutions
1- add html <Base> tag after head..
2- make <head runat="server"> then u can access it from code and add
anything you want here is example

foreach (Control ctrl in this.Master.Controls)
{
if (ctrl is HtmlHead)
{
HtmlTitle title = new HtmlTitle();
title.Text = Convert.ToString(hdt.Rows[0]["Title"]);

Literal baseurl = new Literal();
baseurl.Text = string.Format("<base href=\"{0}\">",
ConfigurationManager.AppSettings.Get("BaseURL"));


HtmlMeta desc = new HtmlMeta();
desc.Name = "Description";
desc.Content =
Convert.ToString(hdt.Rows[0]["Description"]);

HtmlMeta keyword = new HtmlMeta();
keyword.Name = "Keyword";
keyword.Content =
Convert.ToString(hdt.Rows[0]["Keyword"]);

Literal additional = new Literal();
additional.Text =
Convert.ToString(hdt.Rows[0]["AdditinalHeadTags"]);

HtmlHead head = (HtmlHead)ctrl;
head.Controls.AddAt(0, title);
head.Controls.AddAt(1, baseurl);
head.Controls.AddAt(2, desc);
head.Controls.AddAt(3, keyword);
head.Controls.AddAt(4, additional);
}



Boris Yeltsin said:
I use Master Pages, so I make use of URL rebasing through the ~
operator,
like this in the <head>:
<link runat="server" href="~/Root.master.css" media="screen"
rel="stylesheet" type="text/css" />

It works perfectly and the ~ gets converted by the server into the
correct
relative path. Great!

Now, I put this in the <head>:
<script type="text/javascript" src="~/Root.master.js"
runat="server"></script>

And everything screws up. It looks like it's trying to compile that .js
file
into the page as the CLR throws up syntax errors and compiler errors in
my
JavaScript (which is valid JavaScript).

What am I doing wrong? :(
 
G

Guest

Hi again,

I'm running from the ASP.NET web server, so the root of the application
isn't the root of the server, e.g.:
http://localhost:4629/MyApplication/Home.aspx

So I can't just a straight / without rebasing it with the ~ (or another
method)

Thanks again for trying!

Islamegy® said:
i don't know why u don't add it dynamicly like you do in meta tags, so u
don't have to change it the masterpage or forms, u can store it fixed in
web.config and add it from code behind.. easy huh?

you masy also right it like this
<script type="text/javascript" src="/scripts/master.js">
when use just "/" it's mean relative path from the root..

That's all i have..




Boris Yeltsin said:
Hi Islamegy,

1) I'm not sure this will work for me. Doesn't the <base> tag need an
absolute URL? Even if it takes a relative URL, I'll still have to do
something to update the 'href' attribute on the fly to make it point to
the
right level, such as:
<base href="~/" runat="server" />
(which doesn't work - I tried it - but ASP.NET doesn't parse the ~)

2) I'm using code-behind solutions for adding dynamic meta-tags, but
that's
because they're dynamic. My JavaScript file is fixed, so I'd much prefer
the
reference in the markup and not in the code-behind :)

Thanks...

Islamegy® said:
Why you try to make it harder...
There is more simple solutions
1- add html <Base> tag after head..
2- make <head runat="server"> then u can access it from code and add
anything you want here is example

foreach (Control ctrl in this.Master.Controls)
{
if (ctrl is HtmlHead)
{
HtmlTitle title = new HtmlTitle();
title.Text = Convert.ToString(hdt.Rows[0]["Title"]);

Literal baseurl = new Literal();
baseurl.Text = string.Format("<base href=\"{0}\">",
ConfigurationManager.AppSettings.Get("BaseURL"));


HtmlMeta desc = new HtmlMeta();
desc.Name = "Description";
desc.Content =
Convert.ToString(hdt.Rows[0]["Description"]);

HtmlMeta keyword = new HtmlMeta();
keyword.Name = "Keyword";
keyword.Content =
Convert.ToString(hdt.Rows[0]["Keyword"]);

Literal additional = new Literal();
additional.Text =
Convert.ToString(hdt.Rows[0]["AdditinalHeadTags"]);

HtmlHead head = (HtmlHead)ctrl;
head.Controls.AddAt(0, title);
head.Controls.AddAt(1, baseurl);
head.Controls.AddAt(2, desc);
head.Controls.AddAt(3, keyword);
head.Controls.AddAt(4, additional);
}



I use Master Pages, so I make use of URL rebasing through the ~
operator,
like this in the <head>:
<link runat="server" href="~/Root.master.css" media="screen"
rel="stylesheet" type="text/css" />

It works perfectly and the ~ gets converted by the server into the
correct
relative path. Great!

Now, I put this in the <head>:
<script type="text/javascript" src="~/Root.master.js"
runat="server"></script>

And everything screws up. It looks like it's trying to compile that .js
file
into the page as the CLR throws up syntax errors and compiler errors in
my
JavaScript (which is valid JavaScript).

What am I doing wrong? :(
 
S

Steven Cheng[MSFT]

Hi Boris,

since <script > tag is a particular one, adding "runat=server" will not
work for it (<script runat="server" > is used for server-side code block).
For your scenario, a simple way is to embed some server-side code
expression to output the absolute url(from application root directory). e.g.


===============
..............
<head runat="server">
..........

<script language="javascript" src='<%= ResolveUrl("~/scripts/myscript.js")
%>'></script>
.............
</head>
<body>
===============

the "~/scripts/" path will be resolved by the "Page.ResolveUrl" method and
always put the right path to the "script" subfolder under application's
root directory.

Hope this helps.


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Thanks Steven. I hoped there was another way, but that suits my needs for now.

Now, if the ASP.NET team could figure a way of being able to put script
inside attributes and keep the double-quotes on the attributes, that would be
great. It makes my View Source look ugly when one attribute on the page is
single-quoted and the rest are double ;)

I'll override the Render and change it back :p

Thanks again,
Boris
 
S

Steven Cheng[MSFT]

Hi Boris,

Yes, the single/double quote switch does be a headache when we mix
server-side code with html in the aspx template.

BTW, for the <script> tag here, I found that we can use nested double quot
without design-time or runtime error. e.g.

<script type="text/javascript" src="<%= ResolveUrl("~/scripts/myscript.js")
%>"></script>

I am using a C# page, is this also the case on your side? If so, at least
you can avoid mixing single/double quote for these <script > tags here :).

Thanks for your posting.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

OK Steven,

I think I'm going to cause you further headache.

Everything was going so well until I tried to modify the Header object on
the page and hit this:
"Error: The Controls collection cannot be modified because the control
contains code blocks (i.e. <% ... %>)"

from my call:
Page.Header.Controls.Add(.....)

Any other ideas for that style tag?
(I have 2 solutions in my head I don't want to use.. (1) some sort of
literal, (2) override the render and wedge the style tag in)

On the bright side, you were right about the double-quotes. They work in
this instance - I don't know why I've had problems with them in the past.
(I'm using VB.NET - the superior choice ;)

And you were right to change to type="text/javascript" - I think that
language attribute is depreciated now. (I'm using XHTML1.1)

- Boris
 
S

Steven Cheng[MSFT]

Thanks for the followup Boris,

I'd admit that this is a headache and I haven't expected this error since I
never do the two things together before :(. Anyway, good to get it as
earlier as possible so that we will have to find a solution on it.

After some research, I'm afraid the general <%= %> render exception can not
be used together with code which dynamically modify "Header" control's
Controls collection. currently we have the following options:


1. Use databinding expression (<%# %>) instead of <%= %> render expression
to expose dynamic url in header's child controls. <%# %> databinding
expression is constructed at runtime while <%= %> is processed at compile
time. e.g.


===== in master page aspx=========
<head>
...........
<script type="text/javascript" src="<%#
ResolveUrl("~/scripts/myscript1.js") %>" ></script>
<script type="text/javascript" src="<%#
ResolveUrl("~/scripts/myscript2.js") %>" ></script>

</head>
================

=== in master page code behind==========
public partial class Master_site : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
Page.Header.DataBind();

}
====================

We need to explicitly call "DataBind" method of Header to make the <%# %>
databinding expression get evaluated.



2. For script tags, instead of embed them in <header> section, you can add
them into <form> element, just as those <script> blocks registered through
Page.ClientScript.XXXX methods. e.g.


===============
<form id="form1" runat="server">
<div id="divScripts" runat="server">
<script type="text/javascript" src="<%=
ResolveUrl("~/scripts/myscript3.js") %>" ></script>
<script type="text/javascript" src="<%=
ResolveUrl("~/scripts/myscript4.js") %>" ></script>
</div>
........................

===============

Hope this helps some.



Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
 

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

Top