Home > database >  How to redirect to a website in Godot Web
How to redirect to a website in Godot Web

Time:11-07

I'm planning on trying to see if it's possible to make a website in Godot(Yes, I know I shouldn't I just want to try just to try). I thinking about and looking over the features I need and I have one problem.

I just need a way for a person to press a button and get redirected to my itch games. I don't care if it creates a new tab or changes the current tab. Thank you for any help.

CodePudding user response:

If you dont export to web you can call

OS.shell_open("url")

Sadly this does not work in an html export. A solution I found for myself is the JavaScript Interface. As the name suggested it allows you to execute Javascript.

So to open a URL you could connect the pressed signal of a button to something like this:

if OS.has_feature('JavaScript'):
    JavaScript.eval("""
        window.open('https://google.com', '_blank').focus();
    """)

This will open a new tab in the active browser.

I also found an article on the godot site, basically asking the same question (https://godotengine.org/qa/46978/how-do-i-open-richtextlabel-bbcode-links-in-an-html5-export). Here they tried to use an RichTextLabel with BBCode.

The solution did not work for me, when I tested it, though.

CodePudding user response:

As pointed in the comments you can try OS.shell_open, for example:

OS.shell_open("https://example.com")

That only works if it is not an HTML export.


Your other alternative is to eval JavaScript, for example this navigates the current tab:

JavaScript.eval("window.location.href='https://example.com'")

Which only works if it is an HTML export.


Since that only works for an HTML export and the other does not work on an HTML export... If you need both you can do this:

if OS.get_name() == "HTML5":
   JavaScript.eval("window.location.href='https://example.com'")
else:
   OS.shell_open("https://example.com")

See also Close a game in Godot.

  • Related