skip to Main Content

I have a webpage dat greets you with welcome {name}. But the {name} has to be replaced with a name/word at the end of the url. The best I would find is after the # but something else is welcome.

I have tried to look up the different parts and put them together. But I don’t know much about Javascript and it display the script that pulled the url instead of the output of that script.

2

Answers


  1. var hash = location.hash.substr(1); gets the value after the #

    Feels like this might have been asked before.

    Login or Signup to reply.
  2. You can use JavaScript to extract the word and manipulate the DOM to update the label’s content.

    HTML:
    
    
    
     <label id="myLabel"></label>
    
    
    
    JavaScript:
    
    // Get the word behind the '#' in the URL
    
    
     const word = window.location.hash.substring(1);
    
    // Update the label's content
    
    
     const label = document.getElementById('myLabel');
        label.textContent = word;
    

    In this example, window.location.hash retrieves the portion of the URL after the # symbol. The substring(1) method is used to remove the # symbol from the string, leaving only the word behind it.

    Then, the textContent property of the label element is updated with the extracted word, making it display between the label tags.

    Make sure to place this JavaScript code after the label element in your HTML file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search