Results 1 to 2 of 2

Thread: [RESOLVED] [JavaScript] Get url

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,762

    Resolved [RESOLVED] [JavaScript] Get url

    I am needing to get the page name from my webpage's URL. The way that my URL is formatted is:
    To give you a bit of background as to why I'm wanting this is because currently what I'm doing is setting a 'next' button and a 'prior' button to navigate back and forth between lessons without needing to go back to my lessons webpage. The HTML is the same on each webpage and that looks like this:
    HTML Code:
    <footer>
    	<button id="btnPrevious">Go back to 'Type of Projects'</button>
    	<button id="btnNext">Go forward to 'Option Strict'</button>
    </footer>
    The CSS is the same as well:
    Code:
    button {
        border: 1px #245ec6 solid;
        border-radius: 5px;
        margin: 2%;
        moz-border-radius: 5px;
        webkit-border-radius: 5px;
    }
    
    #btnPrevious {
        float: left;
    }
    
    #btnNext {
        float: left;
    }
    How I'm determining A) if the page needs to show the btnPrevious or btnNext buttons and B) the innerHTML of the buttons is by setting the body's id to it's order in the lessons. The JavaScript for that is this:
    Code:
    window.onload = function() {
        var urls = ["ide.html", "projects.html", "new.html", "strict.html", "declaration.html", "code.html", "output.html", "input.html", "comment.html", "arithmetic.html", "logic.html", "if.html", "selectcase.html", "operators.html", "loops.html", "for.html", "foreach.html", "do.html", "concatenation.html", "substring.html", "collections.html", "array.html", "list.html", "dictionary.html", "methods.html", "function.html", "sub.html", "parameters.html", "conversion.html", "typeconversion.html", "parse.html", "tryparse.html", "container.html", "structure.html", "class.html", "event.html", "generatingevents.html", "creatingevents.html"];
        var names = ["The Integrated Developing Environment", "Type of Projects", "Starting a New Project", "Option Strict", "Declaring Variables", "Running Your Code", "Output", "Input", "Comment", "Arithmetic", "Introduction to Conditional Logic", "If Statement", "Select/Case Statement", "Operators", "Introduction to Loops", "For Loop", "For Each Loop", "Do Loop", "Concatenation", "Substring", "Introduction to Collections", "Array", "List", "Dictionary", "Introduction to Methods", "Function", "Sub", "Parameters", "Introduction to Type Conversions", "Type Conversion Functions", "Parse", "Try Parse", "Introduction to Containers", "Structure", "Class", "Introduction to Events", "Generating Events", "Creating Events"];
        var id = Number(document.body.id);
    
        previousUrl = urls[id - 1];
        nextUrl = urls[id + 1];
    
        //Hide the previous or next button
        //Depending on if the id is the first or last id
        if (id === 0) {
            document.getElementById("btnPrevious").style.visibility = "hidden";
            previousUrl = "";
    
            //Only use the next button
            document.getElementById("btnNext").innerHTML = "Go forward to '" + names[id + 1] + "'";
    
        } else if (id === urls.length) {
            document.getElementById("btnNext").style.visibility = "hidden";
            nextUrl = "";
    
            //Only use the previous button
            document.getElementById("btnPrevious").innerHTML = "Go back to '" + names[id - 1] + "'";
    
        } else {
            //Use both buttons
            document.getElementById("btnPrevious").innerHTML = "Go back to '" + names[id - 1] + "'";
            document.getElementById("btnNext").innerHTML = "Go forward to '" + names[id + 1] + "'";
        }
    What I'm wanting to do instead is get the page name from the URL and then get it's index from the url array. This away, whenever I add or delete lessons, I don't have to worry about updating every HTML body ID number.
    Last edited by dday9; Aug 19th, 2014 at 02:43 PM. Reason: forgot the / in my closing quote tag
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,762

    Re: [JavaScript] Get url

    I was able to get the page by using this function:
    Code:
    function getPageName() {
    	var url = window.location.pathname;  
    	return url.substring(url.lastIndexOf('/') + 1);      
    }
    And I modified getting the ID to this:
    Code:
            var urlName = getPageName();
    	var id = urls.indexOf(urlName);
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width