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

Time:01-05

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  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 :)

CodePudding user response:

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"

CodePudding user response:

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

<a  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  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://fw.mycompany.org:9080/ips/block/webcat?cat=1025&pl=0&url=aHR0cDovL3plbmRlc2suY29tLw~~

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~~"
}

CodePudding user response:

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);

  • Related