Uncaught TypeError: Cannot read property ‘split’ of undefined – hoping for a lesson

This has been asked many times before, but I’m a novice an very keen to learn so I was hoping someone could take the time to explain this to me. I also haven’t been able to work out the solution to this particular error using the currently available answeres…

The page in question is www.royalhippodrome.com/kids-summer-activities/beyond-the-stage-rht

The image gallery that should be displayed under the Image Gallery title is not showing and the error is:

Uncaught TypeError: Cannot read property 'split' of undefined
at HTMLAnchorElement.<anonymous> (scripts.js:1604)
at Function.each (jquery.js:2)
at a.fn.init.each (jquery.js:2)
at markScrollToTargets (scripts.js:1602)
at HTMLDocument.<anonymous> (scripts.js:1552)
at i (jquery.js:2)
at Object.fireWith [as resolveWith] (jquery.js:2)
at Function.ready (jquery.js:2)
at HTMLDocument.K (jquery.js:2)

Could someone tell me how to fix this? And, why it happened?

I believe this is the relevant code:

function markScrollToTargets () {

        var scrollToSelector = '#';
        var $aTags = $('nav a');    // get all level anchor tags
        var url = window.location.href;
        var urlSplitArray = url.split(scrollToSelector);
        url = urlSplitArray[0];
        var $target;

        $aTags.each(function (index) {
            var $this = $(this);
            var splitArray = $this.attr('href').split('-');
            var thisURL = splitArray[0];
            var thisTargetClass = splitArray[1];

            if (typeof thisTargetClass != "undefined") {    // check if link has a scrollto selector
                if (thisTargetClass != "") {    // check if link has scrollto selector but no actual target class (like just #-link)
                    if (thisURL != "") {    // if link has url before scrollto selector
                        if (thisURL == url) {   // only consider if the link url is the same as current url
                            $("." + thisTargetClass).addClass('scroll-to-target').addClass('scroll-to-target-class-' + thisTargetClass);
                        }
                    } else {
                        $("." + thisTargetClass).addClass('scroll-to-target').addClass('scroll-to-target-class-' + thisTargetClass);
                    }
                }
            }

        });

Read more here: Uncaught TypeError: Cannot read property ‘split’ of undefined – hoping for a lesson

Leave a Reply

Your email address will not be published. Required fields are marked *