neutralinojs icon indicating copy to clipboard operation
neutralinojs copied to clipboard

Anchor tags with target attribute are not being loaded

Open pathange-s opened this issue 2 years ago • 8 comments

Expected Behaviour
On clicking the link, the corresponding website/app should be opened in default browser.

Actual Behaviour
On clicking the link, neutralino app doesn't respond.

Steps to Reproduce the Problem

  1. npm install @neutralinojs/neu
  2. npx @neutralinojs/neu create .
  3. open /resources/index.html and add the following line <a href="https://neutralino.js.org/">Visit NeutralinoJs !</a>
  4. npx @neutralinojs/neu run
  5. click the link, it opens neutralino.js.org
  6. open /resources/index.html and replace the previous line with <a href="https://neutralino.js.org/" target="blank">Visit NeutralinoJs !</a>
  7. click the link, it doesn't open anything.

Specifications

  • NeutralinoJs Version: 4.3.0
  • NeutralinoJs CLI version: 3.2.0
  • Platform: Ubuntu 20.04

pathange-s avatar Mar 10 '22 18:03 pathange-s

This is a expected behaviour, since Neutralino doesn't bundle the browser and uses the inbuilt browser's API (webview) for showing html css and js, it doesn't know how to treat that url, other than going to that url.

To open URLs in default browser you need the OS API.

Neutralino.os.open("https://google.com");

pegvin avatar Mar 10 '22 20:03 pegvin

So, when target="blank" is set, shouldn't it by default call Neutralino.os.open("https://google.com"); under the hood ?

pathange-s avatar Mar 11 '22 09:03 pathange-s

No, Neutralino Doesn't touch anything under the hood.

pegvin avatar Mar 11 '22 09:03 pegvin

Cool. So, if someone wants a link that needs to be opened externally in web browser, should have to use Neutralino.os.open("https://google.com"); ?

pathange-s avatar Mar 11 '22 12:03 pathange-s

Yes, or you can automate this with this simple hack:

Neutralino.events.on("ready", () => {
    let anchorLinks = document.querySelectorAll("a")
    anchorLinks.forEach((anchorLink) => {
        let href = anchorLink.href;
        anchorLink.onclick = () => {
            Neutralino.os.open(href);
        }
        anchorLink.href = "#"
    })
})

Now you can have a simple anchor link in your html like this:

<a href="https://neutralino.js.org/docs">Docs</a> &middot;
<a href="https://www.youtube.com/watch?v=txDlNNsgSh8&list=PLvTbqpiPhQRb2xNQlwMs0uVV0IN8N-pKj">Tutorial</a>

And when click them, it will open in the default browser.

pegvin avatar Mar 11 '22 13:03 pegvin

By default, can we get this implemented in the framework itself ? Because some developers just use target="blank" and this might leave them to write this piece of code by themselves. So, shall I work on putting this in the framework ?

For ex : if some developer is copying a html file which has <a href="https://neutralino.js.org/" target="blank">Visit NeutralinoJs !</a> he has to write extra lines.

pathange-s avatar Mar 11 '22 13:03 pathange-s

can i work on this issue

harshithgupta28 avatar Mar 28 '22 17:03 harshithgupta28

@harshithgupta28 I faced this issue and raised it. A fix was already suggested, check here. Let me know if you have any other idea to be implemented in your mind.

pathange-s avatar Mar 28 '22 18:03 pathange-s