Skip to content
Advertisement

Variable link that modifies the index of my website

I am looking for the method that allows to modify a value/text on my home page with the used link.

For example, if the URL is mywebsite.com/index.php?name=Mike

somewhere on my website, it will say

“Welcome Mike

If the URL is mywebsite.com/index.php?name=Mark, it will automatically change to

“Welcome Mark

without changing anything in my code.

Is it possible with HTML only or do I need PHP?

Advertisement

Answer

This is possible with HTML, but you need JavaScript. Here’s an example:

// Find the query
let query = window.location.search;
// Extract the name
let match = query.match(/name=([^&]+)/);
// If the name exist, put it in the body
if (match) document.body.innerHTML = match[1];

Note that this won’t work here, but it will work in the website.

As @JNa0 said, PHP is better suited to this task. The PHP would look like echo $_GET["name"];

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement