Jump to content

How do I redirect to target _blank?


kaz
 Share

Recommended Posts

I have a URL field that should be redirected externally via target=_blank

if ($page->external_url) {
    $session->redirect($page->external_url);
}

Is there any solution to add target?

Link to comment
Share on other sites

Posted (edited)

What do you want to happen with the original tab? A redirect is a specific type of HTTP response that directs the browser to go somewhere else. If you want to show a response in the original tab and additionally open a new tab immediately, you can try adding some javascript to the original response, such as window.open('https://example.com', '_blank'), but generally browsers/users don’t like this very much and their popup blockers will prevent it by default.

And why not handle the problem earlier, when you show the link to the user?

if ($page->external_url) {
    echo "<a href='{$page->external_url}' target='_blank'>{$page->external_url}</a>";
}

This likely won’t be blocked because it’s a direct result of a user interaction.

Edited by Jan Romero
Link to comment
Share on other sites

@Jan Romero I have already tried this out. It does not work. It's not a redirect, the link is displayed as a text link in the body. Not what my client needs.

Link to comment
Share on other sites

Two doctors would like to have two external links on one page, which are to an external provider for appointments. I have built a redirect template and will display these links in the sidebar.

Link to comment
Share on other sites

1 hour ago, kaz said:

would like to have two external links on one page

2 hours ago, kaz said:

the link is displayed as a text link in the body. Not what my client needs

Wouldn't these two statements be contradictory? ?

A redirection always change the current page. What you need is a link with target='_blank' like @Jan Romero wrote.

  • Like 1
Link to comment
Share on other sites

I write what the client wants. Why discuss a client's wishes. If it can't be implemented technically with PW, I set these menu links manually in an template.
The question is answered, thanks.

Link to comment
Share on other sites

1 hour ago, kaz said:

I write what the client wants

The problem, for what I understand, is that you are asking for links that open in another tab. It's simple, @Jan Romero already answered the question. But then you say you need a redirect, but a redirect always change the current page URL, it's never opening in another tab. We are not talking about ProcessWire limitations but web standards.

Maybe just be more precise on what you want, or maybe it's just me who doesn't understand... I asked ChatGPT what you need (with a link to this topic), and here is what he answered to your request, a JS script that automatically open a new tab on the wanted URL:

echo "<script type='text/javascript'>
        window.open('{$page->external_url}', '_blank');
        window.location.href = '{$page->some_other_url}';
      </script>";

 

Link to comment
Share on other sites

Posted (edited)
4 hours ago, kaz said:

If it can't be implemented technically with PW

With all due respect, disparaging PW over this is entirely uncalled for. It’s not a ProcessWire concern. Whether it’s links or redirects, your issue lies in interfacing with the browser. ProcessWire’s session()->redirect() is just a thin abstraction over 

HTTP/1.1 301 moved permanently
Location: http://example.com

If a browser doesn’t want your site to open a new tab, it’s not going to open a new tab. I’m sure you’ve seen sites of questionable repute apply some trick or other to open pop-ups, pop-unders and whatever else, and you can use ProcessWire just fine to deliver those same tricks, but ProcessWire’s role in that ends at helping you manage and assemble the data for your response.

Admittedly I’m still not certain what your clients’ goal is. You have a clickable link in a sidebar and want it to open in a new tab, but somehow it also involves a redirect? Sorry I can’t be of more help, but I’m sure we can get it figured out if it’s at all possible.

Edited by Jan Romero
  • Like 1
Link to comment
Share on other sites

6 hours ago, kaz said:

I set these menu links manually in an template.
The question is answered, thanks.

Now I am curious how you solved that.

Can you post a snippet of your solution, please.

  • Like 1
Link to comment
Share on other sites

@wbmnfktr Of course, I solved it this way, it's a bit more work.
I added URL fields to the global settings. These fields are output as simple ul list, that's it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...