javascript - Strange behaviour of included JS file in node.js -
i making node app in current state simple, issue can't figure out.
the generated html of express app looks this:
<html style="" class=" js no-touch svg inlinesvg svgclippaths no-ie8compat"> <head> ... <script type="text/javascript" src="/javascripts/mouse.js"></script> ... </head> <body class="antialiased" style=""><div class="row"><div class="row"> <script> var mouse; var x = 0; var y = 0; window.onload = function() { alert(mouse());//=> undefined mouse = mouse(); mouse.setmousemovecallback(function(e){ //this line fails: "cannot call method 'setmousemovecallback' of undefined" x = e.x; y = e.y; alert("move"); }); } ... </html>
the include script tag in header adds javascript file:
function mouse(onobject){ var mousedown = [0, 0, 0, 0, 0, 0, 0, 0, 0], mousedowncount = 0; var mousedowncallbacks = [0, 0, 0, 0, 0, 0, 0, 0, 0]; var mousetracer = 0; var traceposarray; var target = onobject; if(!onobject){ onobject = document; } onobject.onmousedown = function(evt) { mousedown[evt.button] = 1; mousedowncount--; if(mousedowncallbacks[evt.button] != 0){ mousedowncallbacks[evt.button](evt); } } onobject.onmouseup = function(evt) { mousedown[evt.button] = 0; mousedowncount--; } var mousemovecallback; onobject.onmousemove = function(evt){ //tracing mouse here: if(mousetracer){ traceposarray.push([evt.pagex,evt.pagey]); } if(mousemovecallback){ mousemovecallback(evt); } } var mousewheelcallback; onobject.onmousewheel = function(evt){ if(mousewheelcallback){ mousewheelcallback(evt); } } var mouseovercallback; onobject.onmouseover = function(evt){ if(mouseovercallback){ mouseovercallback(evt); } } var mouseoutcallback; onobject.onmouseout = function(evt){ if(mouseoutcallback){ mouseoutcallback(evt); } } //todo: there bug when move mouse out while pressing button. key still counted "pressed" though release outside of intended area //note: might have fixed bug. further investigation making smaller element. this.anydown = function(){ if(mousedowncount){ for(p in mousedown){ if(mousedown[p]){ return true; } } } } this.setleftdowncallbackfunction = function(fun){ mousedowncallbacks[0] = fun; } this.setrightdowncallbackfunction = function(fun){ mousedowncallbacks[2] = fun; } this.setmiddledowncallbackfunction = function(fun){ mousedowncallbacks[4] = fun; } this.setspcifieddowncallbackfunction = function(num, fun){ mousedowncallbacks[num] = fun; } this.leftdown = function(){ if(mousedowncount){ return mousedown[0] != 0; } } this.rightdown = function(){ if(mousedowncount){ return mousedown[2] != 0; } } this.middledown = function(){ if(mousedowncount){ return mousedown[4] != 0; } } this.setmousemovecallback = function (callback){ mousemovecallback = callback; } this.setmousewheelcallback = function (callback){ mousewheelcallback = callback; } this.setmouseovercallback = function (callback){ mouseovercallback = callback; } this.setmouseoutcallback = function (callback){ mouseoutcallback = callback; } this.enabletracer = function(){ traceposarray = new array(); mousetracer = 1; } this.getmousetracerdata = function() { return traceposarray; } }
so: have assured js file loaded. none less not able access mouse()
method in window.onload
function in body, shows undefined
. strange me because if create html file same thing able access it.
what going on here? there magic trickery in node.js / express disables kind of interaction?
additional notes:
- i using jade template engine.
- the "mouse.js" file works. have used several times before.
- i using socket.io communication between client , server.
complete header:
<head><title>my awesome title</title><link rel="stylesheet" href="/stylesheets/foundation.min.css"><link rel="stylesheet" href="/stylesheets/normalize.css"><link rel="stylesheet" href="/stylesheets/style.css"><script type="text/javascript" src="/socket.io/socket.io.js"></script><style type="text/css"></style><script type="text/javascript" src="/javascripts/jquery.min.js"></script><script type="text/javascript" src="/javascripts/vendor/custom.modernizr.js"></script><script type="text/javascript" src="/javascripts/vendor/zepto.js"></script><script type="text/javascript" src="/javascripts/mouse.js"></script><meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"><meta name="viewport" content="width=device-width"></head>
that throw error if mouse function undefined. reason alert shows 'undefined' mouse function doesn't return anything, hence undefined. should used constructor, in 'new mouse()'
Comments
Post a Comment