44

I don't understand why I get an error message using the substring method to declare a variable.

I want to use the first part of the URL in a comparison.

Site: http://www.elizabet.nl/wordpress

This is the part that's going wrong:

var currentLocation = document.location,
muzLoc = currentLocation.substring(0,45),
prodLoc = currentLocation.substring(0,48), 
techLoc = currentLocation.substring(0,47); 

The error: "currentLocation.substring is not a function"

But this part of the code is fine:

var URL = $(this).attr("href").substring(2) + ' #main';

All of the code:

jQuery(function($){

    var siteURL = "http://" + top.location.host.toString() + "/wordpress", // Declareren van URL van de website.
        URL = '', // Declareren van een URL, welke dan ook. 
        currentLocation = '',
        muzLoc = '',
        prodLoc = '',
        techLoc = '',               
        allLinks = $('a[href^=' + siteURL + ']' ), // Declareren van alle menu-links. Het teken ^ betekent 'begint met'.
        otherLinks = $('a[href^=' + siteURL + "/wp-content" + ']'), 
        siteLinks = $(allLinks).not(otherLinks),      
        mainDiv = $("#content"),
        hash = window.location.hash,
        muziekURL = "http://www.elizabet.nl/wordpress/#/muziek_pf/",
        productieURL = "http://www.elizabet.nl/wordpress/#/productie_pf/",
        techniekURL = "http://www.elizabet.nl/wordpress/#/techniek_pf/";    

    if (hash) {
        hash = "/wordpress" + hash.substring(1); // substring methode haalt karakters van je string af. In dit geval de #, vanwege de offset=1.
        URL = hash;
        $(mainDiv).load(URL);           
    }

function pageLoad() {

                var allLinks = $('a[href^=' + siteURL + ']' ),
                otherLinks = $('a[href^=' + siteURL + "/wp-content" + ']'), 
                siteLinks = $(allLinks).not(otherLinks); 

                siteLinks.each(function() {             

                    $(this).attr("href", "#" + this.pathname.substring(10));
                    })

                    .click(function() {
                    var URL = $(this).attr("href").substring(2) + ' #main';
                    $(mainDiv).load(URL, function(){ 

                    var currentLocation = document.location,
                        muzLoc = currentLocation.substring(0,45),
                        prodLoc = currentLocation.substring(0,48), 
                        techLoc = currentLocation.substring(0,47);                  

                if (muzLoc == muziekURL) {              
                $("body").animate({ backgroundColor: "#151C07"}, 500);
                $(".nieuws").animate({ borderBottomColor: "#99CC33"}, 500);  
                $("#stripe_trans").add("#header").animate({ backgroundColor: "#99CC33"}, 500);
                $("#tabtekst_3").stop().animate({ backgroundColor: "#B8860B" }, 500);
                $("#tab_3").add("a.gold").stop().animate({ color: "#B8860B" }, 500);
                $("#tabtekst_4").stop().animate({ backgroundColor: "#765AAD" }, 500);  
                $("#tab_4").add("a.purple").stop().animate({ color: "#765AAD" }, 500);                                    
                }

                else if (prodLoc == productieURL) {     
                $("body").animate({ backgroundColor: "#251B02"}, 500);  
                $(".nieuws").animate({ borderBottomColor: "#FFCC33"}, 500);  
                $("#stripe_trans").add("#header").animate({ backgroundColor: "#FFCC33"}, 500);                  
                $("#tabtekst_2").stop().animate({ backgroundColor: "#6B8E23" }, 500);  
                $("#tab_2").add("a.green").stop().animate({ color: "#6B8E23" }, 500);   
                $("#tabtekst_4").stop().animate({ backgroundColor: "#765AAD" }, 500);  
                $("#tab_4").add("a.purple").stop().animate({ color: "#765AAD" }, 500);
                } 

                else if (techLoc == techniekURL) {      
                $("body").animate({ backgroundColor: "#181223"}, 500);  
                $(".nieuws").animate({ borderBottomColor: "#B39BE4"}, 500);  
                $("#stripe_trans").add("#header").animate({ backgroundColor: "#B39BE4"}, 500);          
                $("#tabtekst_2").stop().animate({ backgroundColor: "#6B8E23" }, 500);  
                $("#tab_2").add("a.green").stop().animate({ color: "#6B8E23" }, 500);                       
                $("#tabtekst_3").stop().animate({ backgroundColor: "#B8860B" }, 500);
                $("#tab_3").add("a.gold").stop().animate({ color: "#B8860B" }, 500);
                } 

                else {
                $("body").animate({ backgroundColor: "#202020"}, 500);  
                $(".nieuws").animate({ borderBottomColor: "#FFF"}, 500);  
                $("#stripe_trans").add("#header").animate({ backgroundColor: "#FFF"}, 500);             
                $("#tabtekst_2").stop().animate({ backgroundColor: "#6B8E23" }, 500);  
                $("#tab_2").add("a.green").stop().animate({ color: "#6B8E23" }, 500);               
                $("#tabtekst_3").stop().animate({ backgroundColor: "#B8860B" }, 500);
                $("#tab_3").add("a.gold").stop().animate({ color: "#B8860B" }, 500);
                $("#tabtekst_4").stop().animate({ backgroundColor: "#765AAD" }, 500);  
                $("#tab_4").add("a.purple").stop().animate({ color: "#765AAD" }, 500);                                          
                }

                pageLoad();
            });             
        });
}

pageLoad();


}); // End document ready function.
user229044
  • 232,980
  • 40
  • 330
  • 338
Hannemaatje
  • 473
  • 1
  • 4
  • 9

5 Answers5

52

document.location is an object, not a string. It returns (by default) the full path, but it actually holds more info than that.

Shortcut for solution: document.location.toString().substring(2,3);

Or use document.location.href or window.location.href

random
  • 9,774
  • 10
  • 66
  • 83
riffnl
  • 3,248
  • 1
  • 19
  • 32
  • When I use `toString()`converts my string to `[object Object]` (anything I have in the string, is replaced with that). Do you know how to fix it? – Chris May 21 '19 at 22:58
  • @ChristopherAlatorre depends on what object you're toString-ing..if it is an object with no apparent overload you will always get [object Object] as a result. For more information see: https://stackoverflow.com/questions/27649368/objects-convert-using-js-tostring-method-gives-odd-result – riffnl May 22 '19 at 23:11
6

You can use substr

for example:

new Date().getFullYear().toString().substr(-2)
Vivekanand Panda
  • 832
  • 12
  • 23
6

try this code below :

var currentLocation = document.location;
muzLoc = String(currentLocation).substring(0,45);
prodLoc = String(currentLocation).substring(0,48); 
techLoc = String(currentLocation).substring(0,47);
cizario
  • 3,995
  • 3
  • 13
  • 27
3

you can also quote string

''+document.location+''.substring(2,3);
dazzafact
  • 2,570
  • 3
  • 30
  • 49
0

I am picking up on the subject of this post because we have a similar problem. A piece of code that we can't seem to fix. Here is our code

function addLink() {
    var e,
        n = document.getElementsByTagName("body")[0],
        t = (e = window.getSelection()) + ("<a href='" + document.location.href + "'>.</a>"),
        o = document.createElement("div");
    (o.style.position = "absolute"),
        (o.style.left = "-99999px"),
        n.appendChild(o),
        (o.innerHTML = t),
        e.selectAllChildren(o),
        window.setTimeout(function () {
            n.removeChild(o);
        }, 0);
}

we have to remove the last character of the variable T before adding the dot to it with the href tag But if we use a substring function, we have a "substring is not a function" error

regards

Community
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 22 '21 at 16:35