Reputation: 121
im quite new to OOP in javascript and here is my external js file.
(function ($, connection) {
"use strict";
var conn = connection.serverHub;
connection.hub.url = 'http://localhost:51283/signalr';
function trackUser(username, roleid, sessionid, ipaddress) {
var page = document.location.pathname;
conn.server.joinUser(username, roleid, ipaddress, page, sessionid).done();
}
// signalr client method
conn.client.receivedNotification = function (item) {
alert(item);
conn.server.notifiedUser().done();
};
$(function () {
connection.hub.start().done(function () {
trackUser('<%= session("USERNAME")%>', '<%= session("ROLE")%>', '<%= Session.SessionID%>', '<%=Request.UserHostAddress%>');//da attivare solo in produzione
}).fail(function () { alert("failed"); });
}); }(jQuery, $.connection));
and this is how i call my method
<body>
<script src="http://localhost:51283/Scripts/jquery-1.9.1.min.js"></script>
<script src="http://localhost:51283/Scripts/jquery.signalR-1.0.0-rc2.min.js"></script>
<script src="http://localhost:51283/signalr/hubs"></script>
<script src="myexternaljs.js"></script>
<script>
trackUser('<%= session("USERNAME")%>', '<%= session("ROLE")%>', '<%= Session.SessionID%>', '<%=Request.UserHostAddress%>');//da attivare solo in produzione
</script>
</body>
when i tried to call trackUser method and i'm getting trackUser in not defined
where i did wrong?
thanks in advance
Upvotes: 1
Views: 513
Reputation: 40448
It is undefined, because it is not in the global scope. Either add window.trackUser = trackUser
inside your anonymous function or move your function declaration to the global scope.
Upvotes: 0
Reputation: 129782
Your function is defined inside an anonymous scope (function ($, connection) { }
). It will not be accessible outside of that scope. You could create a global variable, and assign your function to that:
window.trackUser = function(username, roleid, sessionid, ipaddress) {
...
};
Upvotes: 4