Forum Moderators: open

Message Too Old, No Replies

Init and var

         

toplisek

7:33 pm on Jan 12, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have set var to invoke function:
var Allapps1 = function () {

function TrackMessages1() {

}

}();

How to set init() in the correct way and outside this file like sample:
<script type="text/javascript">
jQuery(document).ready(function() {
AllApps1.init();
});
</script>
Need help

Fotiman

8:34 pm on Jan 12, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




var Allapps1 = function () {
function TrackMessages1() {
}
}();

When that runs, Allaps1 will be assigned the return value of your anonymous function, which in this case is nothing. You probably want something more like this:

var Allapps1 = function () {
function TrackMessages1() {
}
return {
init: function () {
TrackMessages1();
}
};
}();


In this case, the anonymous function returns an object literal which contains an init function which has access to TrackMessages1 (but TrackMessages1 is not accessible outside of Allapp1).

toplisek

8:01 pm on Jan 13, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What will be the case if there are many ID's like:
TrackMessages1
TrackMessages2
TrackMessages3
and init()

TrackMessages1.init();
TrackMessages2.init();
TrackMessages3.init();

and block init if needed:
//TrackMessages1.init();

Fotiman

8:34 pm on Jan 13, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Not sure if I fully understand what you mean, but maybe something like this:

var Allapps1 = function () {
function TrackMessages1() {
}
function TrackMessages2() {
}
function TrackMessages3() {
}
return {
init: function (cfg) {
if (cfg.tm1) TrackMessages1();
if (cfg.tm2) TrackMessages2();
if (cfg.tm3) TrackMessages3();
}
};
}();


// init with TrackMessages1 and TrackMessages3 only
Allapps1.init({
tm1: true,
tm3: true
});

toplisek

8:35 am on Feb 10, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



How to make it in a simple way:
var Myapplications1 = function () {

function Redirection1() {
}

function Tracking1() {
}
return {
init: function () {
Redirection1();
Tracking1();
}
};
}();

When I have tested it will not work.

Fotiman

11:57 am on Feb 10, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Myapplications1 gets set to the object with the init method. You just need to call it.

var Myapplications1 = function () {

function Redirection1() {
}

function Tracking1() {
}
return {
init: function () {
Redirection1();
Tracking1();
}
};
}();
Myapplications1.init();

toplisek

10:09 pm on Mar 24, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thank you.
I'm not sure but simple redirection will not work when I add function and init.
Is it working if you test?
function myredirectionApp1() {
document.getElementById('msg_redirect_delay1').innerHTML = 'Please wait. You will be redirected within <span id="countDown">10</span> seconds....';
var count = 5;
setInterval(function()
{
count--;
document.getElementById('countDown').innerHTML = count;
if (count == 0) {
window.location = 'http://www.mydomains.com/en-GB/index.html';
}
}, 1000
);
}