will.ditch
will.ditch

Reputation: 49

Get the full URL string via javascript. no luck with window.location.href

I am having an issue getting the full URL via the standard window.location.href method.

The full URL is: "http://fw.mycompany.org:9080/ips/block/webcat?cat=1025&pl=0&url=aHR0cDovL3plbmRlc2suY29tLw~~"

using my script below it will return: "http://fw1.mycompany.org:9080/ips/block/webcat?cat=1025"

the information I actually need is a bit after that "url=aHR0cDovL3plbmRlc2suY29tLw"

code:

<a class="email" title="Email US"  onclick="javascript:window.location.href='mailto:[email protected]?subject=Please review this website&body=The website that needs to be reviewed is located at ' + window.location.href;" style="color:red">click here to e-mail us</a>

any help is greatly appreciated :)

Upvotes: 0

Views: 604

Answers (3)

samthecodingman
samthecodingman

Reputation: 26171

The problem comes from this line, where you are injecting an unencoded URL into an encoded URI:

<a class="email" title="Email US"  onclick="javascript:window.location.href='mailto:[email protected]?subject=Please review this website&body=The website that needs to be reviewed is located at ' + window.location.href;" style="color:red">click here to e-mail us</a>

Using the URL you've provided, the generated URI becomes the following:

mailto:[email protected]?subject=Please review this website&body=The website that needs to be reviewed is located at http://fw.mycompany.org:9080/ips/block/webcat?cat=1025&pl=0&url=aHR0cDovL3plbmRlc2suY29tLw~~

The problem with this URI is that the & symbol (along with a handful of others) has special meaning in URIs. When you look at the parameters of the URI above, you can see that url and pl are parsed as if they weren't in the body parameter:

{
  "subject": "Please review this website",
  "body": "The website that needs to be reviewed is located at http://fw.mycompany.org:9080/ips/block/webcat?cat=1025",
  "pl": "0",
  "url": "aHR0cDovL3plbmRlc2suY29tLw~~"
}

To correctly embed a URL into a URI, you should pass it through encodeURIComponent():

<a class="email" title="Email US"  onclick="javascript:window.location.href='mailto:[email protected]?subject=Please review this website&body=The website that needs to be reviewed is located at ' + encodeURIComponent(window.location.href);" style="color:red">click here to e-mail us</a>

Now when that link is clicked, it produces the following URI:

mailto:[email protected]?subject=Please review this website&body=The website that needs to be reviewed is located at http%3A%2F%2Ffw.mycompany.org%3A9080%2Fips%2Fblock%2Fwebcat%3Fcat%3D1025%26pl%3D0%26url%3DaHR0cDovL3plbmRlc2suY29tLw~~

with the parameters:

{
  "subject": "Please review this website",
  "body": "The website that needs to be reviewed is located at http://fw.mycompany.org:9080/ips/block/webcat?cat=1025&pl=0&url=aHR0cDovL3plbmRlc2suY29tLw~~"
}

Upvotes: 0

Ronnie Smith
Ronnie Smith

Reputation: 18555

It's just the .href property of the URL object.

let str = "http://fw.mycompany.org:9080/ips/block/webcat?cat=1025&pl=0&url=aHR0cDovL3plbmRlc2suY29tLw~~";
let url = new URL(str)
console.log(url.href);

Upvotes: -1

norbekoff
norbekoff

Reputation: 1965

To extract the URL parameter url from the current URL of the page you can use the URLSearchParams.

const searchParams = new URLSearchParams(window.location.search);
const urlParam = searchParams.get('url');

console.log(urlParam); // Outputs "aHR0cDovL3plbmRlc2suY29tLw"

Upvotes: 5

Related Questions