How to detect if website is being viewed in cover page

see title

im writing a small forum template for others to use, but since repl db is persistent in the actual website itself and not in the cover page, I want to redirect to another webpage informing they need to open the repl in a new tab.

i tried searching for answers (read tens of hundreds of topics!) and couldn’t find anything.

Check whether the Referer header says that the user’s opening the site from Replit, then see if window.parent is an instance of Window.

4 Likes

i have no clue how to use javascript :sweat_smile:

2 Likes

Maybe check this site, it’s a good resource:

3 Likes

Well, I guess I could check the headers in the python code running the backend for my website.
But not the window.parent part.

To detect if it’s in the cover page, I’ve seen window==window.parent be used. It checks if the current window (i.e. the page embed) is the same as the parent window (i.e. the main page).

To check referrer, you can use document.referrer and it will tell you the previous page that linked to the current page basically.

Why not? I would just put a small <script> in the HTML file. Then have it redirect to your other page using location.href

2 Likes

Just paste this into your script.js file:

if (!(window.location.host == "forum-template.joecooldoo.repl.co/")) {
  // If the website host is not 'forum-template.joecooldoo.repl.co/'
  window.open("https://forum-template.joecooldoo.repl.co/", '_blank')

  // if you want to open in the same tab, use:
  // window.location.href = "https://forum-template.joecooldoo.repl.co/"
}

If you want me to explain further or need more help, feel free to ask!

Add this code to your html file:

<script>
if (String(window.location).split(".")[1] == "id") {
window.open(window.location) // Open the new tab because it is in the cover page
} else {
continue // It's in a new tab already
}
</script>
1 Like

Unfortunately, this is a template and it’s probably going to be forked several times. I DO have a global Flask template variable that reports the host URL though. Could I replace the URL using the global template variable? Also, this code seems to me that it will activate also if it’s in an iframe from a different website. I want the message to only appear when viewing through the Repl’s cover page.

Would I replace the code with something like this?

if (window.location.host == 'replit.com') {
  window.open('{{host_url}}', '_blank')
//"host_url" template variable gets replaced when a user makes a request to the system
}

In that case, similar to what @Fairies0feast said, use:

var coverPage = window.location.hostname.split('.')[1] === 'id'

if (coverPage == true) {
  // If on cover page
  window.location.href = "path/to/new/webpage";
}

This will ensure that the code works for any user.

2 Likes

I don’t think that works in a script tag

1 Like

That’s another way to do it, but I don’t understand why he can’t use window==window.parent

2 Likes

It does, the templating system replaces every instance, whether it’s in a string or in a value.

so i can embed it in other pages without getting the warning

Just use:

window==window.parent && window.parent.location.hostname=="replit.com"

As the condition

1 Like