-1

For a school work, I have to create a page that changes depending on the clicked link.
To be clearer, I start from a page with different links on it, and whatever the link I click on it brings to the same html page, but its content has to change depending on the clicked link with JavaScript.

What I thought was the easiest way, was to get the id of the clicked link on click, and, depending on it, the page content would change, but I can't keep this value through pages change.

Do you have an idea? (By the way, I am authorized to seek help for this project).

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Jo_Byr
  • 1
  • 2
  • 1
    Are we talking about a single page application? You could use the hash-part of the url to determine what content to show. – Thomas Nov 14 '20 at 13:14
  • It sounds like you might need the [history API](https://developer.mozilla.org/en-US/docs/Web/API/History_API). But please clarify the requirements a bit more, because if it's just a simple different content depending on the url then there is no need to use the history api at all – yi fan song Nov 14 '20 at 13:22

3 Answers3

0

Something like this should get you started.

function myFunc(e){
  e.preventDefault();
  let aa = event.target.innerText;
  console.log(aa)
  const msg = document.getElementById('msg');
  msg.innerText = 'Hello: ' + aa;
  msg.classList.add('wow');
}
.wow{
  width: 90%;
  padding: 20px 50px;
  margin-top: 20px;
  font-size: 3rem;
  color: firebrick;
  background: wheat;
  border:1px solid orange;
}
<a href="bob" onclick="myFunc(event)">Bob</a>
<a href="sam" onclick="myFunc(event)">Sam</a>
<div id="msg"></div>
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • This seems to be what I need but will it work through pages change ? I mean, the links, here "Bob" and "Sam", are href in my cases, so the load of a new page may reset all this no ? – Jo_Byr Nov 14 '20 at 14:32
0

A basic example using HTML and CSS to show different parts of the page depending on the hash.

.page {
    display: none;
}

.page:target {
    display: block;
}
<a href="#page1">Show Page 1</a><br>
<a href="#page2">Show Page 2</a><br>


<div class="page" id="page1">
<h2>Content 1</h2>
</div>

<div class="page" id="page2">
<h2>Content 2</h2>
</div>

And a basic example using the hashchange event.

addEventListener("hashchange", function(event) {
  console.log("Show Content for", location.hash);
});
<a href="#page1">Show Page 1</a><br>
<a href="#page2">Show Page 2</a><br>
Thomas
  • 11,958
  • 1
  • 14
  • 23
  • When I use that, nothing displays in the console as we change page. And that's what I try to avoid – Jo_Byr Nov 14 '20 at 14:53
  • @Jo_Byr The first snippet is an example that you can achieve that completely without JS . The second example currently logs the hash-change. There you could replace the `console.log` with some code that would update the page. – Thomas Nov 14 '20 at 16:24
0

I misunderstood your question. Please ignore my first answer - but I will leave it there for your reference.

What you are looking for is called query strings (sometimes called get strings). Query strings are appended to the url in the format:

https://example.com?varname=varvalue&nuthervar=different value

So, you would configure your anchor tags like this:

<a href="next_page_to_view.html?name=bob">Bob</a>
<a href="next_page_to_view.html?name=sam">Sam</a>

On the other page, next_page_to_view.html in this example, you can have code similar to:

let qs = window.location.search;
const urlParams = new URLSearchParams(qs);
const name = urlParams.get('name')
if (name === 'bob'){
   alert('hi bob');
}

Use the javascript example provided in my first, incorrect, answer to schlep a user-specific message into a div (instead of the alert() message).

Reference:

https://www.sitepoint.com/get-url-parameters-with-javascript/

Is there any method to get the URL without query string?

cssyphus
  • 37,875
  • 18
  • 96
  • 111