0

I'm running a shiny app with an original URL "https://shinyapps.io/myapp", and a custom URL "http://mydomain/myapp". I want to redirect all http connections to the original https URL since otherwise users won't be able to use geolocation in the app but I'm a real newbie in JavaScript and I cannot get it done.

I'm trying to test the URL using JavaScript code in the of my app, but URL is redirected in all cases as if my condition was always true :

var protocol = location.protocol;
if (protocol == "http") {
  window.location.assign("https://shinyapps.io/myapp")
}

What is wrong with this ? I had answers suggesting correcting protocol = "http" to protocol == "http", which I did, but my test is now always false instead of always true... I guess problem comes from how to write "http" on the right side of the equality, I tried "http:", and "http://" but it didn't change anything. Any clue ? Thanks for your help

DCTN
  • 1
  • 2
  • 1
    `=` is used for assignment, use `==` or `===` for comparing – Abito Prakash Jun 27 '23 at 09:09
  • You should be comparing strings in JavaScript using `==` or `===`, but _not_ plain `=`, which is just the assignment operator. Most likely, your `if` condition is always returning true because the assignment is happening. – Tim Biegeleisen Jun 27 '23 at 09:09

1 Answers1

0

Don't use = for comparison.

Use ==or === instead.

Here your condition protocol = "http"is actually and assigment and is always true.

bschaffh
  • 114
  • 8
  • Thanks for your answer but protocol == "http" now returns always false, because of the way I write "http" I guess, I tried "http:", "http://" but it is still always false. – DCTN Jun 27 '23 at 11:27
  • what is the value of `location.protocol` ? – bschaffh Jun 27 '23 at 11:29
  • console.log(location.protocol) http: undefined – DCTN Jun 28 '23 at 10:15
  • So the condition (location.protocol == "http:") should be true as far as i've understood correctly the problem – bschaffh Jun 28 '23 at 12:17
  • My problem is : condition (location.protocol == "http:") is always false, or is not tested, as redirection never happens... – DCTN Jun 29 '23 at 06:01
  • Edit : redirection never happens because Shinyapps with custom URL are displayed inside an iframe which prevents scripts from running... – DCTN Jun 30 '23 at 08:29