1

So I have the following it works perfectly fine in most browsers except in IE8 and older it will only run when you first visit page or when you ctrl + f5(clear cache) and view the page. What is happening is the item this affects is in my master layout/template for the page so when I traverse through the site it doesn't load.

For instance I am on home page, I click into personal section expect the item to load on the next page but it doesn't even appear.

$(document).ready(function () {

    if (window.document.domain == "developer10.machine") {

        var oScript = document.createElement('script');
        oScript.type = 'text/javascript';
        oScript.src = '/js/uk_converter.js';

        // most browsers
        oScript.onload = function () {
            renderCurrencyConverter('GBP', 'EUR', 1.00);
        }

        // IE
        oScript.onreadystatechange = function () {
            if (this.readyState == 'loaded' && ($.browser.msie && parseInt($.browser.version, 10)))
            {
                renderCurrencyConverter('GBP', 'EUR', 1.00);
            }
        }
        document.body.appendChild(oScript);
    }
});

1 Answer 1

3

As soon as you set the src of the script, which is already in cache, it finishes loading. Since you add the onload handler after you set the src, the onload handler never gets called. Try setting the src after you have already set the handlers.

$(document).ready(function () {

    if (window.document.domain == "developer10.machine") {

        var oScript = document.createElement('script');
        oScript.type = 'text/javascript';

        // most browsers
        oScript.onload = function () {
            renderCurrencyConverter('GBP', 'EUR', 1.00);
        }

        // IE
        oScript.onreadystatechange = function () {
            if (this.readyState == 'loaded' && ($.browser.msie && parseInt($.browser.version, 10)))
            {
                renderCurrencyConverter('GBP', 'EUR', 1.00);
            }
        }

        oScript.src = '/js/uk_converter.js';
        document.body.appendChild(oScript);
    }
});

Or try jquery script loader:

$(document).ready(function () {
    $.getScript("/js/uk_converter.js", function () {
        renderCurrencyConverter('GBP', 'EUR', 1.00);

    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Could you possibly provide an example?
Well yeah please, I just want to make sure I am doing what your thinking in your head.
@Anicho perhaps try using JQuery's script loader api.jquery.com/jQuery.getScript It allows you to set a handler for when a script is successfully loaded and works cross browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.