简体   繁体   中英

Rewriting one part of a URL into a link

I am trying to get one part of a URL to insert into another.

URL in address bar will read domain.com/test.php?/12345 then link in page must become domain.com/do/Login/12345 (this end number will change for every user)

(Note that I can't change the URL domain.com/test.php?/12345 as this has already been set by the site owner.)

For the URLs above I had to remove the http://www . part above as new user can't submit more than one link for spam prevention. The final script should include http://www .

You will be able to see that all I need to get is the number at the end ie 12345 but I'm not too sure how to achieve this (javascript beginner). This is what I have so far:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Path test</title>
</head>

<body>

<script>
// get URL eg http://www.domain.com/test.php?/12345
newURL = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;
</script>

<script>
//write new domain then add user number ie 12345
document.write('http://www.domain.com/do/Login/' + window.location.pathname);
</script>
</body>
</html>

This obviously does not work but shows where I got to.

I've had some luck using a script I found at http://www.sitepoint.com/forums/showthread.php?t=244955 but it only worked with filenames with an extension name on like .html, .php etc.

Any ideas?

Thanks in advance.

First, you need to get the part of the url after the question mark. This is available in the

 window.location.search

variable. Note that that already includes the "/". Then you need to create the actual link. document.write('http://...) won't work because that will just write it in text form and will not be an actual anchor tag. For that you need something like:

document.getElementById("yourLink").href = "http://domain.com/do/Login" + window.location.search

don't forget to put an anchor tag in the html with an id that matches the element in the getElementById() method above. If you don't want to put an anchor tag in your html markup, you can create one with javascript with something like:

var link = document.createElement('a');
link.href = "http://domain.com/do/Login" + window.location.search;
document.body.appendChild(link);

This may need further adjusting to your needs.

Could this do the work? It uses the whole URL, splits it from the '?' (meaning from the start of the parameters) and then concatenates the parameters to the base URL.

var oldUrl = window.location.href;
var parameters = oldUrl.substring(oldUrl.indexOf('?',0),oldUrl.length);

var newUrlBase = 'http://www.domain.com/do/Login/';
var newUrl = newUrlBase + parameters;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM