WordPress.org

Ready to get started?Download WordPress

Forums

WP YouTube Lyte
[resolved] Uncaught TypeError: ... has no method 'getElementByID' (10 posts)

  1. Eric McNiece
    Member
    Posted 2 years ago #

    Hello again!

    My plugin suddenly stopped working. It's returning an error:
    Uncaught TypeError: Object Mon Nov 21 2011 10:44:03 GMT-0800 (Pacific Standard Time) has no method 'getElementById'

    from line 324, which is this:
    var bU='http://pinwheelmusic.com/wp-content/plugins/wp-youtube-lyte/lyte/';var d=document;if(d.addEventListener){d.addEventListener('DOMContentLoaded', insert, false)}else{window.onload=insert} function insert(){if(!d.getElementById('lytescr')){lytescr=d.createElement('script');lytescr.async=true;lytescr.id='lytescr';lytescr.src='http://pinwheelmusic.com/wp-content/plugins/wp-youtube-lyte/lyte/lyte-min.js?wylver=0.9.2';h=d.getElementsByTagName('script')[0];h.parentNode.insertBefore(lytescr, h)}};

    The function I use to call the player is this:

    <?php 	if(get_field('music_videos')){
    					if(function_exists('lyte_parse')) while(the_repeater_field('music_videos')) echo lyte_parse(str_replace('http://', 'httpv://', get_sub_field('youtube_url')));
    				}
    		?>

    Any ideas as to where to start looking? As always, thanks for your awesome support :)

    http://wordpress.org/extend/plugins/wp-youtube-lyte/

  2. futtta
    Member
    Plugin Author

    Posted 2 years ago #

    a wild guess; you (or some of the javascript code you wrote/ an other plugin generates) store the date in d, while the lyte javascript wants to use it as shorthand for "document"? in which case d.getElementById indeed would not work as the string has no method by that name?

  3. Eric McNiece
    Member
    Posted 2 years ago #

    Haha, awesome guess... earlier on the page there's a jQuery date picker with new Date(d.getFullYear(), 1-1, 1).

    Is it safe to change the variable for Youtube Lyte? Or maybe should I insert a chunk of code to clear d?

  4. futtta
    Member
    Plugin Author

    Posted 2 years ago #

    you can change the variable for youtube lyte, but you'll have to re-apply that change when upgrading off course. so some code to clear the "d=new Date();" from the jQuery datepicker could be more futureproof.

    but i'm not sure what the best solutin would be, best to try and see what happens i guess.

  5. Eric McNiece
    Member
    Posted 2 years ago #

    Alright... sorry to keep bugging you, but I'm stumped again. I'm playing with the code:

    <script type="text/javascript">delete d; var d=document; alert(d);</script>

    The date d var is being created in the head; the document d var is being used in the body. I've tried placing this code all over the place, but it's still throwing that error. I need another hint :)

    Should I be placing this in a script and using wp_enqueue_script?

  6. Eric McNiece
    Member
    Posted 2 years ago #

    It's the MailChimp plugin Version 1.2.6 | By MailChimp and Crowd Favorite. They have a little jQuery routine for a datepicker. I'm not using that particular feature so I changed their variable from 'd' to 'e', but yes, that will only last so long.

    mailchimp.php lines 145, 151, 152.

    I'll have to do some more investigating as to how this can be avoided in the future. It stands to reason that with thousands of plugins some people would use the same variable names... I wonder how they prevent situations like this?

    Thanks for the push.

  7. futtta
    Member
    Plugin Author

    Posted 2 years ago #

    I've always struggled with javascript-scope as well, i always take the trial & error approach ;-)

    Does mailchimp use a datapicker in the frontend (for visitors)? I would expect it to be only used in the backend, so the datepicker jquery plugin shouldn't even be loaded in the frontend html?

    I'll look into javascript scope & closure to see how I could 'protect' wp-youtube-lyte's javascript from "collisions" like this. hopefully I can solve this for a future release.

  8. Eric McNiece
    Member
    Posted 2 years ago #

    Yeah, the datepicker is for the front-end. Turns out there's an option to disable it in the admin, but even so... what if somebody needed it?

    In the quest for education I posted this: http://stackoverflow.com/questions/8219998/prevent-duplicate-javascript-variables

    Very informative! I'm going to start using this for my own projects as well. Fortunately, wp-youtube-lyte is small enough that doing this shouldn't take too long :)

  9. futtta
    Member
    Plugin Author

    Posted 2 years ago #

    indeed, a closure. ain't stackoverflow great? :-)

    i assumed (re-)declaring "var d=document;" would be sufficient to ensure i've got the right object, but apparently not.

    i'll add this on my todo list for 0.9.3 (or, who knows, magical 1.0.0). but as you've got the problem solved, I won't hurry this new version out. don't want to push a 4th version in only a couple of weeks time. people tend to not like updates that are too frequent.

  10. futtta
    Member
    Plugin Author

    Posted 2 years ago #

    update: i've pushed corrected code to trunk on the wordpress plugin svn.

    changed files:

    • wp-youtube-lyte.php
    • widget.php
    • readme.txt
    • lyte/lyte.js
    • lyte/lyte-min.js

    tested on a couple of browsers and all seems to work ok, should be a better solution to your problem. this will be included in the next version of WP YouTube Lyte.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic