4

Sorry, if this is a stupid question, but I'm just trying to have a URL entered into a field and then a button that, when clicked, will take the value in that field and open a new tab to that URL. If I do this:

    onClick={event => {
                      event.preventDefault();
                      window.open('https://www.google.com/', '_blank');
    }}

Then it works just fine and opens a new tab with the specified url.

But if I set urlText = "https://www.google.com/" and do this:

onClick={event => {
                   event.preventDefault();
                   window.open({urlText},'_blank');
}}

Then it opens a new tab, but it goes to this instead:

http://theCurrentWebsiteDomain/[object%20Object]

I assume it's trying to add the urlText to the end of the current domain, like it's a page that I'm specifying instead of a whole new URL.

I added a console.log and it displays urlText just fine. If I click on it from the console, it goes right to the expected website.

What am I doing wrong?

1
  • Have you tried to pass the urlText without curly braces? I think it expects the value as a string: window.open(urlTex},'_blank'); Commented Jul 27, 2022 at 0:00

3 Answers 3

6

There is no need to add the curly braces around your urlText variable since it's within the event callback function. If it were to be used in the JSX then curly braces would be required (like <div>{urlText}</div> or <input type="text" value={urlText} />) - but since you're calling a function there is no need to wrap the variable name in curly braces.

The error is caused because by wrapping the urlText variable in curly braces, you are converting urlText to an object: { urlText: "foo" } and thus it appears as [object%20Object] in the URL bar since any object converted to a string will be literally '[object Object]'.

onClick={(event) => {
  event.preventDefault();
  window.open(urlText, "_blank");
}}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, you all were right. window.open(urlText, "_blank"); worked perfectly. Thanks!
1

Simply pass the string urlText directly into the function: window.open(urlText, '_blank') without curly brackets.

The curly brackets is syntax to create an object, this function takes in a string argument as its first parameter. So javascript parses your argument into a string [object%20Object], and due to there being no protocol in that it recognises it as a local path.

Comments

1

I encountered a similar issue when using window.open(), where the localhost was being appended to the URL, resulting in a format like http://localhost:3000/{url}.

  const handlePreview = (url) => {
    window.open(url, '_blank');
  };
  
To resolve this issue, I prefixed the URL with https:// as a quick workaround:

  const handlePreview = (url) => {
    window.open(`https://${url}`, '_blank');
  };
  

This modification ensured that the URL opened correctly without the unwanted localhost prefix.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.