java <script> include in Master pages - src is not mapped

R

Rolf Welskes

Hello,

I have simple in one folder FolderA

MyMaster.master and
script01.js
styles01.css

The content pages are in

FolderA/FolderB/ContentPage01.aspx
and
FolderA/FolderB/FolderC/ContentPage02.aspx

in MyMaster.master I have so

<link href="styles01.css" type="text/css" rel="Stylesheet" />
<script type="text/javascript" src="script01.js"></script>

Now if I call ContentPage01

I see in the generated source css ref is correct maped to ../styles01.css
but javascript include not, it is script01.js
must be ../script01.ja

The same if I call ContentPage02
I see in the generated source css ref is correct maped to
.../../styles01.css
but javascript include not, it is script01.js
must be ../../script01.ja

I can in my design only use relativ pathes no other.
Why is stylesheet include mapped correctly and javascript include is not ?
What to do?

Thank you
Rolf Welskes
 
M

Mark Fitzpatrick

Rolf,
Try adding the runat="server" attribute to the script tag. The
easier one I do though is placing a Response.Write(Request.ApplicationPath)
inline in the src attribute such as:

<script type="text/javascript" src="<%
Response.Write(Request.ApplicationPath); %>script01.js"></script>

It's a bit old-school, but it works ok.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
S

Steven Cheng[MSFT]

Hello Rolf,

I just find another thread in this group discussing on the similar question:

subject: "It's trying to compile my JavaScript?"


The problem here is actually due to the <script> tag which is a special
case. Those normal header items (like <link> <style> ....) can be marked
with "runat=server" so that server-side will parse their url property.
However, adding "runat=server" will not work for <script> tag because
<script runat="server" > is used for server-side code block.


For your scenario, a simple way to resolve the URL problem 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. Please feel free to let me know if you have any other
questions on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



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

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