java script and master template and pages

Topics: Business Logic Layer, Themes
Jan 6, 2008 at 4:12 AM
Wondering if anyone is running into the same issue here.

I have a site.master that has a javascript. when I set the src = (filename) to work with regular posts it doesn't work when viewing pages. If I change the path for the javascript file so that it works with the page it no longer works with regular posts viewing.

It appears when viewing regular post pages (ie default.aspx) the system sees the javascript file based on the subdirectory structure of the theme you are using. When viewing a page (ie one built with the page editor in admin, the system seems to see the javascript path based on the page folder.

I have tried putting the javascript file at the root of the blogengine app. just to make troubleshooting easier and when referenced there for the site.master to work with default.aspx, it won't work when viewing a page and vice versa.

Does anyone know the scheme on how blogengine wants to see where javascripts are put?

Jan 6, 2008 at 8:58 AM
Would this work for you? Add the script code in the "HTML head section", configured in the admin/settings interface. E.g. <script type="text/javascript" src="/YourScript.js></script>

Hope this helps,
Jan 6, 2008 at 6:14 PM
No. Sorry, it does not work. If I put the script at the root of the app., then for default.aspx it will work if I do the following:

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

but that won't work for a page, ie ..../page/pageone.aspx in the url line.

If I put:

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

That works for the page access ie http://myblog/page/pageone.aspx, but then when you access http://myblog/default.aspx the script is not found.

It works like this no matter if I put the script in the site.master or take it out of the site.master and put it in the header under admin.

If I try and use the <%=Utils.AbsoluteWebRoot %> in the path, ie in site.master I have:

<script type="text/javascript" src=<%=Utils.AbsoluteWebRoot%>code.js></script>

I get a run time error saying basically that this can't be done - ie the following error:

System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

If I try this syntax in the header under admin, it gets put literally in the html without code resolution, ie a view of source on the web page shows:

<script type="text/javascript" src=<%=Utils.AbsoluteWebRoot%>code.js></script>

Don't know what the answer is. Surely there must be others out there that have javascript in the site.master. Anyone got any ideas?


MikevZ wrote:
Would this work for you? Add the script code in the "HTML head section", configured in the admin/settings interface. E.g. <script type="text/javascript" src="/YourScript.js></script>

Hope this helps,

Jan 6, 2008 at 7:37 PM
I'm out on a limb here- if your javascript is not theme-dependent, what about entering references to the js file in the page.aspx and post.aspx files?
Jan 6, 2008 at 8:44 PM
Hi Warren, would you consider trying to put the code.js in the very root of your website (or a root subfolder e.g. /scripts/), because you point out it's a relative path issue, right? Then the code to use is simply:

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

Of course you could use the full absolute path too. So let's say you store your custom .js in your own theme folder and just use:

<script type="text/javascript" src="">

My apologies if I misunderstood your issue.

Jan 7, 2008 at 1:30 AM
Edited Jan 7, 2008 at 1:31 AM

Yes, if you do that it works. In simple cases that would be OK, but in more complex cases it again breaks down as you are forced to break up a javascript file and manage it under different circumstances. As "elegant" as the code is in blogengine I suspect this has been addressed somewhere, its just that I can't seem to figure out out blogengine wants to see things, javascript wise.
Jan 7, 2008 at 2:06 AM

Two things:

One: Apologies not need. I probably did not explain it correctly. Your suggestion is actually the issue at hand, ie. the path set one way like src="/scripts/code.js" will not work for both regular post pages (default.aspx) and pages from the admin at the same time. regular pages (default.aspx) want the path one way and pages developed under admin want the path another way so if they both reference the same site.master file both types of pages won't work at the same time.

two: referencing an absolute path like src="http://mywebsite/code.js" does work, it just seems to me that blogengine would not require that to be. It works for an interium solution, but I suspect somewhere in the "innards" of blogengine it is capable of handing javascript relative to the root, I just ain't figured it out yet.

Probably a bad posting habit, but i'm being lazy, and since I have a post already open, I'll pose another issue.

When you develope pages under the admin section of blogengine and include tagging it gets dropped even when you create it under the html view. For example, if I insert a postview tag just like on the default.aspx page when you update the html it gets dropped. Now if I develop that same page structure under WLW and post the page it seems to take it OK. Any thoughts there? That is, what is the acceptable html tagging (including tags) when developing pages under the admin section. If you develop a regular page (ie mypage.aspx) there is no problem, but if you try to duplicate that same page under admin anything other than plain vanilla html seems to get dropped.

This also works for javascript code inserted as attributes of html tags. For example, if I reference a mouseover event in an html tag (say adding some sub-menuing items on a page) they get dropped when the page content is developed under the blogengine admin area. They seem to be accepted under WLW.

Has that been your experience?

Thanks for the dialogue...


Jan 7, 2008 at 10:04 AM
Edited Jan 13, 2008 at 2:39 PM
Hi Warren, as for the script path, that actually works consistent at my side as long I don't use relative paths (both just MyScript.js or ../OtherScript.js). It works as long I start the path from the root (/somefolder/somescript.js). The include method seems irrelevant either: it works for both using the HTML head section as the site.master file.

Personally I like to add overall scripts in the HTML head admin section, so that's theme independent (and works for posts and pages too). Perhaps a BE.Net development champ can shed some light on your issue.

I share your experience though with dissimilar behavior based on using Windows Live Writer or the BE.Net native add-post/page interface. The latter uses TinyMCE, which might be causing some of your unexpected drops. Although WLW (html mode) seems more consistent, it does surprise me from time to time too. For instance, pasting a Google script works until I switch WLM view mode and it converts the javascript to some iframe html. Very weird frankly.

In closing, for most of that debugging I use Fiddler, giving you more visual insight of what's happening under the hood.

Would you consider using that tool to see what kind of response is returned from BE.Net?

Thank you too for the dialogue!

Jan 7, 2008 at 2:55 PM

That's very interesting. I can't get that to work on my site. If I put the script at the root and have a script tag like:
<script src=code.js ....> it works for default.aspx but not pages. If I change the src attribute to be ../code.js then it works for pages but not for defuault.aspx. Anyway if I use the http://..... it works fine. So, as I understand it you are saying that if you do as I indicate above, it works for you?

I guess my next question would be where is blog engine installed on your site? at the root or a subdirectory as an app. Also, I have not actually uploaded blogengine to an internet hosting service and tested it there. I am testing it on my laptop using web developer express and cassini. Maybe that has something to do with it.
Jan 7, 2008 at 4:25 PM
Warren, please note the very first forward slash in src="/ so your website always looks for that script relatively starting from the URL root

To answer your second question first: I installed my blog engine in a subfolder as an app (/mikesblog/), but to a web visitor the actual URL looks like it's installed in the root, because they see and, so without the /mikesblog/ part.

Talking about paths in ASP.Net can be confusing, because of the application path, physical path and relative webroot path (and usually all 3 have different values). Now to answer your first question, yes the script/code would work as long as I start with that forward slash. So:

1) <script src="/code.js"> always works
2) <script src="code.js"> only works for web pages stored in the same physical path as the .js code (note the missing forward slash)
3) <script src="/somefolder/morecode.js"> always works

(and include type="text/javascript" as well, like <script type="text/javascript" src="/yourscript.js">

Does this make sense?
Jan 7, 2008 at 4:41 PM
Yea, it makes plenty of just doesn't work that way for me. Must be some minor config difference or maybe cassini does not render the html as well as it should, etc. I'll upload the thing to an internet hosting site and see if that makes a difference.

Appreciate all you help.

By the way I was "snooping" around the .cs for the pages.aspx page and notice the line:

if (arg.Body.ToLowerInvariant().Contains("[usercontrol"))

So, it seems with some special tagging you can inject webcontrols into a page. I have "fiddled" with the syntax but can't seem to get it to work. I always get the error: ERROR - UNABLE TO LOAD CONTROL (see line 144 of source .cs).

Have you fiddled with that at all?

Jan 7, 2008 at 4:46 PM
Glad to hear that and good luck in advance with uploading it to your ISP.

Seems you found the user control injection feature introduced since version 1.2.
Can't say I used, tried or fiddled with it, but if you'd like to discover more:
Jan 7, 2008 at 5:21 PM
Boy do I feel silly. Not paying attention to detail I guess.

When running the website on my laptop with VWD and cassini using the file system as the web type....Duh...casinni uses the subdirectory as a subdirectory for the web site.

For example I am running VWD and my website is http://localhost:3046/blogengine8827/default.aspx or .../page/page-seven.aspx.

I'm thinking all the time (without paying attention, of course) that the website is at the root of the host, ie. http://localhost:3046. When I changed my script tag to reflect the blogengine8827 it works like a champ.

Sorry for all the consternation about nothing... Oh! well. At least the mystery is solved.

Jan 8, 2008 at 12:14 PM
The way I do it is that I have this little function in a Utils class

public void AddJavascript(string code, Page p)
HtmlGenericControl script = new HtmlGenericControl("script");
script.Attributes.Add("type", "text/javascript");
script.InnerText = code;

Then I can add it to whatever page I want by doing:
HtmlHelper h = new HtmlHelper();
h.AddJavascript("alert('Hello World!')",Page);

- jonah