I want to grab the 7 digit number from the url of a browser as shown below:
www.example/?id=1122331
and add the 7 digit to another link.
This is the base of the link wwww.referece.com/ref
and change to
wwww.referece.com/ref1122331
.
I want to grab the 7 digit number from the url of a browser as shown below:
www.example/?id=1122331
and add the 7 digit to another link.
This is the base of the link wwww.referece.com/ref
and change to
wwww.referece.com/ref1122331
.
Well for that I would recommend you to use PHP. To get the value of ID in PHP just do the following
<?php
$id = $_GET['id']; //here we grab the 7 digits and place them into $id var
// Now lets build the new link
$link = "www.referece.com/ref".$id; //values are concatenated and we get www.referece.com/ref1122331
?>
Option 1: You can do it client side using javascript, for example how-to-get-the-value-from-the-url-parameter
Option 2: You can do it server side (Java/Php/Python and so on - depend your server side language)
1) You can use PHP to do it from server-side as shown below:
<?php
// Get the url of the browser
$url = isset($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"] : "";
// Get the 7-digit number
$number = strpos($url, "=")>0 ? substr($url, strpos($url, "=")+1, strlen($url)) : "";
// Add the 7-digit number to a new link and return it
return $link = "wwww.referece.com/?ref=" . $number;
?>
2) Or, instead, to do it from client-side you can use JavaScript as follows:
// Get the url of the browser
var link = document.location.href;
// Get the 7-digit number
var number = (link.indexOf("=")) ? link.substr(link.indexOf("=")+1, link.length) : null;
// Add the 7-digit number to a new link and return it
var newLink = "wwww.referece.com/?ref=" + number;
[EDIT]: ↓
// Create button
var button = document.createElement("a");
button.setAttribute("href", newLink);
document.body.appendChild(button);
Most typical solution involves server-side processing, either PHP, ASP, JSP
For JSP
<a href="wwww.referece.com/ref<%= request.getParameter("id") %>"
would do it.
Client-side would be also possible with javascript.
Here is how to override the target of a link:
JavaScript function in href vs. onclick
And here is how to obtain the parameters:
document.location.href
This grab the page url. Lets say the original page ulr is this `https://stackoverflow.com/questions/
user entre this manually on the url. ?mid=123456
new URL `https://stackoverflow.com/questions/?mid=123456
now the goal is to extract this number 123456 and append to the new URL. this is a user pass parameter to action button through URL. each user parameter is unique and the length is always 7 digit.