I'm trying to incorporate a new slideshow on my site. The slideshow has everything I need except the option for "height = "80%"", i.e. I want the slideshow to scale with the browser because the new site design will sort of by like an android application; fully immersive.

Because the slideshow itself doesn't have this option, I'm creating a javascript code that will check the document/browser window size every 2 seconds and reload/resize the slideshow itself so that it always fits the screen. But, the problem is that the javascript will only run once, onload, and doesn't call "setTimeout" after I paste a certain string of code into the script.

So, the problem is that setTimeout actually STOPS working, so it has worked before, after I include this string of code:

var thescript = document.createElement("script");
thescript.type = "text/javascript";
thescript.innerHTML="jQuery.flashgallery('gallery/ArtGallery.swf', 'gallery/gallery.xml', {width: '100%', height: '"+calcheight+"px', background: '#000000'});";
document.getElementById('galleryid').appendChild(thescript);

The full javascript check function is here:

function getDocSpecs() {

    clearTimeout(t);
    var D = Math.max(
        Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
        Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
        Math.max(document.body.clientHeight, document.documentElement.clientHeight));

    var Le = Math.max(
        Math.max(document.body.scrollWidth, document.documentElement.scrollWidth),
        Math.max(document.body.offsetWidth, document.documentElement.offsetWidth),
        Math.max(document.body.clientWidth, document.documentElement.clientWidth));

    calcheight = (0.80 * D);
    alert(preheight + "_" + prewidth + "_" + D + "_" + Le + "_");
    if (preheight != D || prewidth != Le) {

        var thescript = document.createElement("script");
        thescript.type = "text/javascript";
        thescript.innerHTML = "jQuery.flashgallery('gallery/ArtGallery.swf', 'gallery/gallery.xml', {width: '100%', height: '" + calcheight + "px', background: '#000000'});";
        document.getElementById('galleryid').appendChild(thescript);
    }


    preheight = D;
    prewidth = Le;

    t = setTimeout('getDocSpecs()', 2000);
}

These two seem to not like each other:

var thescript = document.createElement("script");
thescript.type = "text/javascript";
thescript.innerHTML="jQuery.flashgallery('gallery/ArtGallery.swf', 'gallery/gallery.xml', {width: '100%', height: '"+calcheight+"px', background: '#000000'});";
document.getElementById('galleryid').appendChild(thescript);

and

t = setTimeout('getDocSpecs()', 2000);

I've tried to trick it by loading the slideshow first then calling the function, adding a click activated text, calling multiple functions, etc.

有帮助吗?

解决方案

The two shouldn't effect eachother, but at the same time, you shouldn't need to use createElement for what you're trying to do.

I've tidied it up a little, separated bits into clear functions and removed the createElement parts. Hopefully you'll be able to debug more easily now. I've tried to keep the behaviour the same otherwise, though.
As mentioned in comments, you could also change to using an event listener for resize, which will save the function from having to be called so often.

var getDocSpecs = (function () {
    var t,
        pre = {h: -1, w: -1},
        getDocSpecs;

    function asyncFlashGallery(p1, p2, p3) {
        return window.setTimeout(function () {jQuery.flashgallery(p1, p2, p3)}, 0);
    }

    function dMax(nx) {
        return Math.max(document.body[nx] || 0, document.documentElement[nx] || 0);
    }

    getDocSpecs = function getDocSpecs() {
        window.clearTimeout(t);
        var D = Math.max(
                dMax('scrollHeight'), dMax('offsetHeight'), dMax('clientHeight')
            ),
            Le = Math.max(
                dMax('scrollWidth'), dMax('offsetWidth'), dMax('clientWidth')
            ),
            calcheight = (0.80 * D);

        alert(pre.h + "_" + pre.w + "_" + D + "_" + Le + "_"); // consider console.log
        if (pre.h !== D || pre.w !== Le) {
            asyncFlashGallery(
                'gallery/ArtGallery.swf',
                'gallery/gallery.xml',
                "{width: '100%', height: '" + calcheight + "px', background: '#000000'}"
            );
        }

        pre.h = D;
        pre.w = Le;

        t = window.setTimeout(getDocSpecs, 2000);
    };
    getDocSpecs.CANCEL = function () {window.clearTimeout(t);}
    return getDocSpecs;
}());
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top