Web Analytics Made Easy -
StatCounter Change Link - CodingForum

Announcement

Collapse
No announcement yet.

Change Link

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Change Link

    Hello,

    i want to change a link of a breadcrumb in a wordpress cms and it won't work...

    Here's the HTML

    HTML Code:
    Code:
    <div class="breadcrumb">
        <ul>
            <li><a href="http://bo-institut.de">Home</a></li>
            <li class="category-detail"><a href="http://bo-institut.de/service/filler-hyaluron/" title="Service">Service</a></li>
            <li class="category-detail active"> <a href="http://bo-institut.de/service/filler-hyaluron/" title="Filler Hyaluron">Filler Hyaluron</a></li>        
        </ul>
    </div>
    The second link (Service) should be changed to 'ؤsthetik' and should have a new href.

    Here's what i got:

    Code:
    <script>
    var cont = document.getElementsByClassName('breadcrumb');
    var el = cont.getElementsByClassName('ul')[1];
    var link = el.getElementsByTagName('a');
    
    link.innerHTML = "ؤsthetik";
    link.href = "http://bo-institut.de/aesthetik/";
    </script>
    Thank you for your help!

  • #2
    Well, first up you probably should be changing that SERVER SIDE, not bloating out the page client side with scripting.

    Second, you don't have any classes named 'ul' -- I think what you meant to do was getElementsByTagName, not getElementsByClassName there... also, link would return an array as well so you'd need to [1] that as well. In fact, ALL those methods would return arrays. (well, technically nodeLists not arrays, but close enough for Gov't work)

    Oh, and if you have multiple var, you don't have to say var every time, comma delimit them.

    Code:
    <script>
    var
    	breadcrumb = document.getElementsByClassName('breadcrumb')[1],
    	ul = cont.getElementsByClassName('ul')[1],
    	a = ul.getElementsByTagName('a')[1];
    
    a.innerHTML = "ؤsthetik";
    a.href = "/aesthetik/";
    </script>
    Though assuming it's turdpress, first thing i'd be trying to do is neuter all the URI's so they aren't filled with absolute links for nothing. If you're serving it from "http://bo-institut.de" you don't need to SAY that, a leading slash is all you need.

    Again, not that ANY of this belongs in client side scripting.
    Last edited by deathshadow; Sep 16, 2016, 07:47 AM.
    Walk the dark path, sleep with angels, call the past for help.
    https://cutcodedown.com
    https://medium.com/@deathshadow

    Comment


    • #3
      Thank you for your reply. Now i changed the code to:

      Code:
      var
      	breadcrumb = document.getElementsByClassName('breadcrumb')[1],
      	ul = breadcrumb.getElementsByTagName('ul')[1],
      	a = ul.getElementsByTagName('a')[1];
      
      a.innerHTML = "ؤsthetik";
      a.href = "/aesthetik/";
      and the console shows this error: Uncaught TypeError: Cannot read property 'getElementsByTagName' of undefined. I don't know, what's the issue. What do you mean with this: Again, not that ANY of this belongs in client side scripting. Sorry, i'm from germany and perhaps i understand something wrong
      Last edited by lulu68163; Sep 16, 2016, 08:25 AM.

      Comment


      • #4
        Of course the they give undefined results as there isn't a second <ul> in the second element with class="breadcrumb" for that code to find.

        Try looking for the first tag in the first occurrence of the class instead. (position [0] instead of position [1])
        Stephen
        Learn Modern JavaScript - http://javascriptexample.net/
        Helping others to solve their computer problem at http://www.felgall.com/

        Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

        Comment


        • #5
          Originally posted by felgall View Post
          (position [0] instead of position [1])
          ... oops, and I missed that, so even the indexes on the one array check was wrong, and I just blindly copied it. Good catch, though the error kind of gave it away.
          Walk the dark path, sleep with angels, call the past for help.
          https://cutcodedown.com
          https://medium.com/@deathshadow

          Comment


          • #6
            Originally posted by lulu68163 View Post
            Again, not that ANY of this belongs in client side scripting. Sorry, i'm from germany and perhaps i understand something wrong
            You are using JavaScript to change the content... that's "client side" code. If the content is being output by the server incorrectly, you change that content ON THE SERVER in whatever it is that's generating that HTML. You don't slop JavaScript at it after the fact!
            Walk the dark path, sleep with angels, call the past for help.
            https://cutcodedown.com
            https://medium.com/@deathshadow

            Comment

            Working...
            X