Posts Tagged ‘Scripts’

Gotcha: must set document.location.hash only at end of page in Mozilla

I just came across a different behaviour between IE and Mozilla Firefox: scripts at the former seem to execute after the page has fully rendered, while the later they seem to execute when they’re met by the page parser or something like that.

The HTML script tag has a defer attribute for running scripts after page has rendered, but that is available only for script tags that load an external script, not for embedded scripts in the HTML page. So this seems like one of those different interpretations that usually plague "standards". 

So, at the following JSP code, I was creating a table of student entries with a named anchor (bookmark) at each of the entries (each named per student id), so that when coming back from another page to that one I could tell it to scroll down to a given student automatically.

The issue was that if I placed the script that set document.location.hash to the student id, then at IE it worked OK, but at Mozilla Firefox it just moved to the top of the page even though the URL at the address bar of the browser had the hash entry (e.g. #55555) at the end and would scroll down correctly if I pressed Refresh button there.

Moving the script to the end of the page made it work for both IE and Mozilla Firefox. Don’t you love HTML’s "debug everywhere"?


<%@ page contentType="text/html" session="true" pageEncoding="UTF-8" %>

response.setHeader("Cache-Control", "no-cache, no-store"); //HTTP 1.1
response.setHeader("Pragma", "no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", -1); //prevents caching at the proxy server

<%@ page import="javax.portlet.*" %>
<%@ page import="java.util.ResourceBundle" %>

<%@ taglib uri="" prefix="portlet" %>
<%@ taglib uri="" prefix="fmt" %>

  RenderRequest renderRequest = (RenderRequest) request.getAttribute("javax.portlet.request");
  RenderResponse renderResponse = (RenderResponse) request.getAttribute("javax.portlet.response");

  String studentId = (String)renderRequest.getAttribute(Constants.JSP_RENDER_STUDENTID);
  if (studentId == null) studentId = "";

       for(int i = 0; i < proposalsCount_supervisor; i++) {
        IProposalListItem proposal = proposals_supervisor.getProposalListItem(i); 
  <a name="<%=proposal.getStudentId()%>"></a>
   <div class="dissertation_subsection<%=(i%2)%>">




<script type="text/javascript"> <%– MAKE SURE THIS IS PLACED AT THE END, ELSE IT WON’T WORK AT MOZILLA –%>
document.location.hash="<%=studentId%>"; //scroll to previously examined student
<%– alert(document.location.hash); –%>

Gotcha: var x = x() in Javascript gives “Object Expected” error

At ClipFlair Studio (a Silverlight app), I had some time ago implemented a confirmation warning upon user trying to close the webpage (when it was running inside the web browser), which then had stopped functioning. It seems at some refactoring I had added code like the following:

var activityView = activityView();

and it was failing with error “Object Expected” (and even worse this was done silently – had to use browser’s debugging tools to catch it – because the respective code was running at onbeforeunload browser window event).

Such code would be fine in C#, but in Javascript it seems that a local variable with name x hides a function named x (with any number of parameters in its definition). This is obviously because functions are first-class objects in Javascript and can be treated like variables themselves too.

All started working again fine after changing that code to:

var a = activityView();

Below is the respective block of Javascript code included in a script tag at the webpage that hosts the Silverlight control. Note the “control.content.activityWindow”, where “activityWindow”  is accessed via the Silverlight HTMLBridge (that object – that has to be marked with ScriptableType class attribute – is registered using that key via HtmlPage.RegisterScriptableObject at the Silverlight app side).

function silverlightControl() {
  return document.getElementById("silverlightControl");

function onSilverlightLoad(sender, args) {
  var control = silverlightControl();
  if (control != null)

function activityWindow() {
  var control = silverlightControl();
  if ( (control != null) && (control.content != null) )
    return control.content.activityWindow;
    return null; //need this so that it doesn't return undefined

function activityView() {
  var a = activityWindow();
  if (a != null)
    return a.GetView();
    return null; //need this so that it doesn't return undefined

function onClosing() {
  var a = activityView();
  if ( (a != null) && (a.WarnOnClosing) )
    return "Do you want to exit ClipFlair Studio?";
//else return undefined is implied (no onClosing message that is) } function onClosed() { var a = activityView(); if (a != null) a.WarnOnClosing = false; } function installEventHandlers() { window.onbeforeunload = onClosing; window.onunload = onClosed; } installEventHandlers();

Launching MATLAB with no UI (headless) for remote automation

I’m sharing here an answer I just gave at:

for archiving, since this may interest fellow Roboticists

Launching MATLAB on a server without the GUI is covered thoroughly at
you should also read the user comments/discussion there

e.g. you can use

start matlab -nosplash -nodesktop –nojvm –noFigureWindows -minimize -r


The –noFigureWindows parameter is mentioned at
where it doesn’t mention –nodesktop (only the UNIX doc mentions it for X-Windows), however it seems you have to use it on Windows too, else you see the MATLAB code editor window too popup.


I’m not sure the –nojvm works at all on Windows version of MATLAB, it’s not mentioned in the official documentation URL mentioned above and also Process Monitor tool from Microsoft SysInternals shows that MATLAB still loads it’s internal Java VM’s files even with that command-line parameter provided.

If not using Java is too restrictive for your needs:

Regarding -nodesktop vs. -nojvm, there is a third
(undocumented/unsupported) option: ‘-noawt’. -noawt loads Java (thus
enabling Java I/O, data structures etc.) and just prevents Java GUI


Also note that when launching MATLAB from a WCF service, even if you set an AppPool to run the service under a custom account (say MATLABUSER) and even if you set the Matlab current directory / search path for that user (Matlab keeps them separately for each Windows user account), it keeps on ignoring them. Thus you are forced to use “-sd” deprectated command-line parameter of Matlab (mentioned at the official doc URL I sited above) to set the startup path of Matlab. In that folder you can have a “startup.m” file where you use ADDPATH command of Matlab to temprorarily update the Matlab startup path for the running Matlab process.

ADDPATH Add directory to search path.
    ADDPATH DIRNAME prepends the specified directory to the current
    matlabpath.  Surround the DIRNAME in quotes if the name contains a
    space.  If DIRNAME is a set of multiple directories separated by path
    separators, then each of the specified directories will be added.
    ADDPATH DIR1 DIR2 DIR3 …  prepends all the specified directories to
    the path.

Unfortunately the “-sd” command-line parameter is deprecated, which means it could be removed in the future, but the Matlab online documentation fails to list alternatives, just says “For information about alternatives, see .” and nothing more there:

matlab -sd "startdir" specifies the startup directory for MATLAB (the current directory in MATLAB after startup). The -sd option has been deprecated. For information about alternatives, see .


BTW, instead of launching MATLAB via Windows shell command you could launch as a COM automation server:

or via existing C API for launching MATLAB:


If you use this often (e.g. from a web service), it is best that you keep an instance of MATLAB in memory all the time (since Windows apps for example share code and have separate data this can spare much time by avoiding the reloading of MATLAB code into memory at every script run). That instance could be headless too (with no UI) using this command at server boot (e.g. by adding an entry at HKLM/Software/Microsoft/Windows/CurrentVersion/Run in the Windows registry using "regedit.exe"):

start matlab -nosplash -nodesktop –nojvm –noFigureWindows -minimize

%d bloggers like this: