0

I have a question and I wonder if it's possible. I want to add dynamically content into an html page, by changing the id in the URL.

I mean, for example I want to add the name of a person to a web page:

At first my page would be : www.mypage.com/index

<p> Hello <span></span> welcome to our page</p> 

The result I want to get, is by writing this url: www.mypage.com/index#YourName the code updates itself to this:

<p> Hello <span id="YourName">YourName</span> welcome to our page</p> 

So the id and the content of the span updates juste by modifying the URL. I don't really know how to achieve that. I wonder if I could give to my span dynamically an id by typing it on the url, and then retrieve this id and use it as a the value of the span.

5
  • Meaning when url is www.mypage.com/index#HerName the content will be <p> Hello <span id="HerName">HerName</span> welcome to our page</p> ? Commented Jan 16, 2021 at 22:37
  • That's exactly what I try to achieve. Commented Jan 16, 2021 at 22:38
  • So the span id change with the hashTag ? Commented Jan 16, 2021 at 22:39
  • Yes! I wonder if that's possible? Commented Jan 16, 2021 at 22:41
  • 1
    Yes, check my answer below Commented Jan 16, 2021 at 22:49

3 Answers 3

1

You can get the ID value from the url by using window.location.hash.

Additionally, you can watch these changes by using the hashchange window event.

function showHash() {
  const newHash = window.location.hash.replace("#", "");
  const resultSpan = document.querySelector(".hash-value");
  
  resultSpan.id = newHash;
  resultSpan.innerHTML = newHash;
}

window.addEventListener("hashchange", showHash, false);
window.addEventListener("DOMContentLoaded", showHash);
<body>

  <div>Current HASH in URL is: <span class="hash-value"></span></div>

  <div class="buttons">
    <a href="#hash1">hash1</a>
    <a href="#hash2">hash2</a>
    <a href="#hash3">hash3</a>
  </div>
</body>

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer, but I think I should update my question.
Did you test the snippet locally?
prnt.sc/wxj992 Is this what you need to achieve?
0

You can use JavaScript to get a current link such as:

<script>
   let YourLink = window.location.href;
   let startingPoint = YourLink.indexOf('#');
   let endingPoint = YourLink.indexOf('/'); // In case if the name is not the last thing on your link
   let NameYouWant = YourLink.substring(startingPoint, endingPoint);

   // So 'NameYouWant' would be the name you need to use
</script>

In case you use '#' for the starting point of every name in your link, hope it's helpful.

2 Comments

Thank you for your answer, but how can I use this to update the content of my span?
@mscmdaddcts you give id to your span like this: <span id='mySpan'></span> and then you add $('#mySpan')[0].innerHTML = NameYouWant; to your script, P.S sorry for late answer
0

Maybe you can try this

let splits = window.location.href.split("#");
let span = document.getElementsByTagName("p")[0].getElementsByTagName("span")[0];
span.innerText = splits[splits.length - 1];
span.id = splits[splits.length - 1];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.