My name is Rob Cherny and I'm a professional Web Developer with 16 years of experience creating Web sites and Web-based applications. This site is where I write about my work, and random other things...

Learn More


Close Tab

Web Development

DomLoaded Updated, Again

Many thanks go to Matthias for pointing out that he’d already resolved the HTTPS issue with a fake “https:///” solution.

Works like a charm. I’ve updated the DomLoaded script (yet again) with a location.protocol test which toggles the URL as appropriate.

The location.protocol test works great so you don’t need to change your script ever and the extra file per Dean’s original post isn’t needed.

Thanks again go to Matthias.

Update: So Matthias has me all up in arms. Turns out that you don't need javascript:void(0) in one environment, and "https:///" in the other. I just assumed that if you were running under https and got an error about http, than the reverse would be true. That is, you'd get an error about https if you were running under http. Guess not. Bug, or feature? I guess why warn about secure content on an insecure page? In principle I still like the detection though, it just seems wrong somehow... Step away from the computer Rob... Try again tomorrow... ;-)

Update 2: Thanks go to Alistair Potts and Matthias, who yet again have figured out the latest issue with this script. The https:/// was failing in IE7b3, but now they've figured out using src=//0 works just fine (so far).

Update 3: Fixed some issues with the way the script was implemented for IE, noted in some comments, either here or on one of the other posts about this script.

Jul 3, 02:47 PM in Web Development (filed under JavaScript)

  1. Matthias Willerich    Aug 17, 03:08 PM    #

    Thanks for wrapping it up so nicely. I love them multiple onloads.

  2. Tonyz    Sep 20, 06:55 PM    #

    in firefox 1.5.07 i have an issue while trying to have multiple calls of domloaded.load() at startup. The browser says that a script tries to start but is yet occupied by another not termined. Probably is something to do with the istances of getelementbyid that are running while the previous ones are not termined. I don’t know as I’m not so expert in js.

  3. rob    Oct 10, 11:48 AM    #

    Tonyz, please post a link to an example, I’ve not been able to reproduce that problem, the script’s working just fine in FF for me.

  4. tonyz    Oct 17, 07:55 PM    #

    thanks rob, now the script is just fine, i had to eliminate one script from loading with domloaded because probably was not compatible with another script but i don’t think this is an issue of dom loaded but of the script itself because with the other ones works great!

  5. Kravvitz    Oct 18, 01:14 AM    #

    I noticed one odd thing in your variant of this script. Shouldn’t this:

    var proto = "javascript:void(0)";

    be the following?

    var proto = "src='javascript:void(0)'";

    It seems to work though.

  6. rob    Oct 18, 10:05 PM    #

    Ugh. You’re right. That was introduced when I fixed the HTTPS stuff for IE. It does seem to work though, although I’m not sure why. That’s odd that it did. Can’t say I know why without a further look, however, I’ve fixed the code just to be sure. Thank you for noticing that.

  7. Ahmad    Feb 12, 08:32 AM    #

    I’m getting a permission denied error when the page loads which is triggered by the following line:
    ‘if (location.protocol == “https:”) proto = “src=//0”;’

    Can u help me 2 avoid this error? Thnx.

  8. rob    Feb 12, 11:51 AM    #

    Ahmad, what browser is giving you the error, MSIE? What version? All? Are you running under HTTPS, locally, or just HTTP?

    I would try removing the “if” portion of the statement, removing the line above and sticking with just the:

    var proto = “src=//0”;

    That’s the critical element of code I think.

    I’d still be curious what the setup of your example where it it blowing up might be.

    Is there a sample link you could provide?

  9. Ahmad    Feb 13, 03:21 AM    #

    thnx 4 ur prompt reply rob :)

    Sorry i 4got 2 specify. It’s ie-stupid-6 sp2. The testing page is HTTP but i have both n the site. Sorry no link its intranet.

    btw: ur suggestion seems 2 have resolved the issue! So thnx. I’m no expert, but would there b any implications with the removal of that line? Because if not maybe u want 2 update ur script 2 do the same :)

    Thnx. 4 sharing.

  10. Ahmad    Feb 13, 05:15 AM    #

    Oh! Now its peompting me 2 enter my internet password which I don’t want 2 have because my site is within the intranet!

  11. rob    Feb 13, 03:31 PM    #

    I actually have a small set of changes and refinements to the script, although nothing that I thought was keeping it from working. The script should be fairly solid as is.

    If you’re being prompted for a password, are you certain there’s no other references on the page to something behind a password? It seems unlikely this script would be the thing doing it. You’d have to remove the script completely to be able to tell.

    Are your MSIE security settings changed for Intranet sites on your domain? There may be an issue there … you may see a different icon in the status bar (bottom right) than when browsing say, this site.

  12. Ahmad    Feb 17, 06:40 AM    #

    “Are you certain there’s no other references on the page to something behind a password?”

    - Yes, it is the: var proto = “src=//0”; that is asking 4 the password. When i changed it 2:

    var proto = “src=‘https:/// ‘”;
    it didn’t prompt me 4 an internet password! Thnx 4 all ur support.

  13. Matt Nordhoff    Mar 11, 11:34 PM    #

    I haven’t used this yet (I don’t onload anything important, so I’m just using a simple function that does not fall back to <script defer> or the timer thing), but…

    Shouldn’t it make sure the initialization code only gets run once?

    In load(), right after push() at the top, add:

    if (arguments.callee.initialized) return;

    arguments.callee.initialized = true;

    Also, the last few lines use spaces instead of tabs for the indentation.

    (What the hell is with this preview box?)

  14. Matt Nordhoff    Mar 12, 04:04 AM    #

    BTW, it would be a minor optimization to replace ‘/loaded|complete/.test(document.readyState)’ with ‘document.readyState == “complete” || document.readyState == “loaded”’. The userAgent thing is faster as it is, though.

  15. Matt Nordhoff    Mar 12, 10:06 AM    #

    For browsers that don’t support Array.prototype.push(), you should distribute it with Ash Searle’s push() implementation (in an “if (!Array.prototype.push)”, of course).

    Sorry for all of the comments. It’s just that this is a great script and I want to do my part to make it better. :)

  16. rob    Mar 12, 12:05 PM    #

    Matt: The load() function calls the loaded() function which contains the arguments.callee.done statement virtually identical to the “initialized” statement you’re suggesting. You’re suggesting it much farther up the chain, but the results are similar.

    I haven’t examined replacing the UserAgent tests, as I freely admit I didn’t write the script from scratch, I just refactored it.

    The Array issue, yeah, well, it depends on the level of browser support you’re going for I guess, so it’s a decent suggestion for certain.

    As I’ve commented, I have some other versions others have submitted as well, but have not had time to parse through them all.

    Thanks for all your input, the more comments and suggestions the better as it keeps the conversation going (as I’ve been a little lacking there myself).

    Oh, is there a problem with the preview box somehow (other than being really, really small?)?

  17. Matt Nordhoff    Mar 13, 05:47 AM    #

    About “initialized”, if you call load() multiple times, it will go through all the addEventListener(), timer, IE, onload stuff every single time, which is pointless.

    About the userAgent and readyState tests, it was only readyState I suggested replacing, and I expect that would be safe. With the userAgent test, it’s case-insensitive (though I dunno why), so it would be harder to rewrite to not use RegExps and may not be as fast.

    About push(), the script goes to the trouble to set window.onload for old browsers, so it might as well not error out on the first line in them. :P

    About the preview box, it wasn’t good about the script tag. I don’t think it HTML-escapes the comment when it puts it in the Message textarea again. And I’m not sure how it handles HTML in comments and the comment system doesn’t seem to parse HTML entities.

    Thanks for getting back to me so quickly. :)

  18. rob    Mar 13, 12:37 PM    #

    Hey Matt,

    You make excellent points in all cases, I’m sure there’s room for improvement, especially where the multiple loads are concerned (the original didn’t have that, I added it) and the push will indeed potentially through an error in older browsers. I won’t argue ;-)

    The preview and comment system here is pretty much the default Textpattern setup with Textile — which is pretty bad for HTML. It should escape entities though … hrmmm … but maybe not.

    Uh, ok, I’ll look into it.

  19. Aaron    Mar 14, 04:40 PM    #

    I am trying to use on our asp pages but have an issue. We load this before anything else on the page before the doctype and it seems that it is keeping the script from executing. I can not remove that comment before the doctype so do you have any suggestions on how to work around this?

  20. Aaron    Mar 14, 06:16 PM    #

    Also i noticed it does not work if you remove the doctype.

  21. rob    Mar 14, 11:05 PM    #

    Aaron, if you have anything before the DTD in some browsers it forces the browser rendering engine into what’s called “quirks” mode. These scripts in all likelihood have not been tested and possibly won’t work in “quirks” mode. They probably assume a valid document in “standards” mode and so the fact that you can’t remove this comment before the DTD limits your options. I would strongly suggest doing everything you can to try to get the comment removed.

    If you attach some code to:

    window.onload = testMode;

    and your function testMode() does:


    It will tell you what rendering mode the browser currently is running in. “CSS1Compat” means standards mode and “BackCompat” means quirks mode.

    If you just can’t get the script working in “quirks” mode (I personally have not tested it) then you’re probably stuck with a plain old window.onload statement for your project.

    If I get some time I’ll look into this some more, but can’t even recall the last time I built something in “quirks” mode. It’s just not recommended practice these days.

    It’s easy to read up on “quirks” mode with a Google search.

  22. Aaron    Mar 15, 05:24 PM    #

    Thanks so much for the information! i really appreciate your help!

    I will see what i can findout. Thanks so much again for the help and great script!!!!

  23. Matt Nordhoff    Mar 25, 06:25 PM    #

    I just noticed a small (syntactical, not functional) bug: In loaded(), it uses “i = 0” instead of “var i = 0”, so i is a global variable (which actually could cause a problem if someone is using such a variable).

  24. Robin    Jul 24, 08:01 AM    #

    the src=//0 trick seems to break IE5.5, but luckily my release copy of IE7 seems to work fine with https:/// so I’ve swapped it back to that. Anyone confirm this?

  25. Ed Eddington    Sep 26, 02:59 PM    #

    My tests of src=//0 in IE6 with IEWatch show this creates a request to https://0/ which gives ERROR_INTERNET_TIMEOUT. In my tests, this causes no problems. However, a user of my site is seeing the requests hitting his proxy server (bound for IP where they cause the page to hang for 5 minutes until they timeout. Not sure if the problem occurs with any proxy config – or just this user’s.

Commenting is closed for this article.

In This Section