0

I have a Javascript function for Marquee animation in my ASP.NET Masterpage that runs fine when the page is first loaded, but then stops when a page button is pressed. Any idea how to fix this?

I got help from here, Javascript Marquee to replace <marquee> tags

Using the second answer

window.addEventListener('load', function () {
    function go() {
        i = i < width ? i + step : 1;
        m.style.marginLeft = -i + 'px';
    }
    var i = 0,
        step = 3,
        space = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
    var m = document.getElementById('marquee');
    var t = m.innerHTML; //text
    m.innerHTML = t + space;
    m.style.position = 'absolute'; // http://stackoverflow.com/questions/2057682/determine-pixel-length-of-string-in-javascript-jquery/2057789#2057789
    var width = (m.clientWidth + 1);
    m.style.position = '';
    m.innerHTML = t + space + t + space + t + space + t + space + t + space + t + space + t + space;
    m.addEventListener('mouseenter', function () {
        step = 0;
    }, true);
    m.addEventListener('mouseleave', function () {
        step = 3;
    }, true);
    var x = setInterval(go, 50);
}, true);

HTML

<div id="marquee">
    1 Hello world! 2 Hello world! <a href="#">3 Hello world!</a>
</div>

CSS

#marquee {
    background:#eee;
    overflow:hidden;
    white-space: nowrap;
}
  • Please include **your** code in the question itself. Whay do you mean "page – Jon P Aug 02 '16 at 00:18
  • Thank you Jon, I've updated it with the code now. By page, I mena I press a button that is not on the Masterpage, but one of the pages on my website, say Edit Users, and this is when the Javascript function and it's animation fail. I can see see the "marquee" text, but it stops moving. Any advice would be great, thanks for responding. – Russell Waggoner Aug 02 '16 at 17:33
  • @RussellWaggoner Is the button you're pressing within an UpdatePanel or something similar, that does not perform a full-page postback? – Tyler Roper Aug 02 '16 at 17:55
  • @TylerRoper Yes, the button is in an Update panel. Thanks for responding and anyhelp would be greatly appreciated. – Russell Waggoner Aug 02 '16 at 18:00
  • I will type you an answer now, please hold on just one minute :) – Tyler Roper Aug 02 '16 at 18:06
  • @TylerRoper Thanks for the code, still having a problem, I'll comment below – Russell Waggoner Aug 02 '16 at 18:56

1 Answers1

1

This problem relates to your button being within an UpdatePanel as you mentioned in the comments above. An UpdatePanel will cause the page to postback without re-triggering the PageLoad event, so any javascript tied to that will be lost.

However, you can use a PageRequestManager. This will allow us to also trigger a function on UpdatePanel postback. This allows us to run the pageload script both on initial load and UpdatePanel postback. The only caveat is that the script block has to come after your ScriptManager. I'd suggest moving it just before your </body> if possible.

Basic example:

//Add a page request manager
var prm = Sys.WebForms.PageRequestManager.getInstance();

//On postback of UpdatePanel
prm.add_endRequest(function () {
    alert("UpdatePanel was just triggered!");
});

//On pageload
window.addEventListener('load', function () {
    alert("Initial page load!");
}, true);

Your example:

//Because we now have to call our function on both Page Load AND UpdatePanel postback, let's put it in function
function pageInit() { 
    function go() {
        i = i < width ? i + step : 1;
        m.style.marginLeft = -i + 'px';
    }
    var i = 0,
        step = 3,
        space = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
    var m = document.getElementById('marquee');
    var t = m.innerHTML; //text
    m.innerHTML = t + space;
    m.style.position = 'absolute'; // http://stackoverflow.com/questions/2057682/determine-pixel-length-of-string-in-javascript-jquery/2057789#2057789
    var width = (m.clientWidth + 1);
    m.style.position = '';
    m.innerHTML = t + space + t + space + t + space + t + space + t + space + t + space + t + space;
    m.addEventListener('mouseenter', function () {
        step = 0;
    }, true);
    m.addEventListener('mouseleave', function () {
        step = 3;
    }, true);
    var x = setInterval(go, 50);
}

//Add a page request manager
var prm = Sys.WebForms.PageRequestManager.getInstance();

//On postback of UpdatePanel
prm.add_endRequest(function () {
    pageInit();
});

//On pageload
window.addEventListener('load', function () {
    pageInit();
}, true);

If there are certain things you only want to happen on the initial load and not on the UpdatePanel postback, you can take them out of the pageInit() function and put them back in your load event instead.

Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
  • Thank you for the code, I'm still not there yet. The animation does not play like the PageInit() function is not being called. But I do have an error in the Chrome Javascript Console on this line, var prm = Sys.WebForms.PageRequestManager.getInstance(), Uncaught ReferenceError: Sys is not defined(anonymous function) @ Default.aspx:91 – Russell Waggoner Aug 02 '16 at 19:07
  • @RussellWaggoner Try moving the script block to the end of your file before `

    ` instead of being the head.

    – Tyler Roper Aug 02 '16 at 19:09
  • Making progress, not quite there. Moving the javasript below just before the

    did get the animation of the marquee to work, but I'm still having the same problem with the button click on the page, it is still stoping the animation. I still get the javascript error that Sys is not defined. Any help with this would be great, thanks again.

    – Russell Waggoner Aug 02 '16 at 19:16
  • @RussellWaggoner The script block has to be *after* your ScriptManager. Is this the case, and if not, is it possible to put your ScriptManager earlier in the page? – Tyler Roper Aug 02 '16 at 19:17
  • Nevermind, it's working now like I wanted. Thank you so much for this! Awesome! – Russell Waggoner Aug 02 '16 at 19:25
  • @RussellWaggoner Glad I could help! Please accept my answer above if you wouldn't mind :) I don't generally like to remind people, however seeing as you may be slightly new on SO - Accepting someone's answer provides them with additional reputation and abilities around the site. – Tyler Roper Aug 02 '16 at 19:25
  • You're right, I lurk here but only posted like once. I marked your answer with the green check mark so I think that did it Tyler. Thanks again. – Russell Waggoner Aug 02 '16 at 19:33
  • @RussellWaggoner You rock! Thanks, and no problem. – Tyler Roper Aug 02 '16 at 19:34