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

  • Thread starter Thread starter Rolf Welskes
  • Start date Start date
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
 
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
 
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
 
Back
Top