Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am creating a website using google sites.The website consists of a main page and a couple of subpages.
I want the code to display 'hello world' on the main page when a specific subpage is loaded.
I have embedded the code bellow on the main page:

<!DOCTYPE html>
<html>
<body>

<h1>The iframe element + CSS</h1>

<p>An iframe with default borders:</p>
<iframe src="https://sites.google.com/view/restocknormal/home/qqq2" id="iframe">
</iframe>

<script>
  iframe.onload = function() {
   
    iframe.contentDocument.body.prepend("Hello, world!");
  };
</script>
</body>
</html>





google shows the error message google.sites.com refused to connect

Thank you in advance. Any tip or advice helps.

What I have tried:

I am just starting to learn javascript. I red about the iframe element on w3schools.com, and I ran this code on their site:

<!DOCTYPE html>
<html>
<body>

<h1>The iframe element + CSS</h1>

<p>An iframe with default borders:</p>
<iframe src="/default.asp" width="100%" height="300">
</iframe>

<p>An iframe with a thin black border:</p>
<iframe src="/default.asp" width="100%" height="300" style="border:1px solid black;">
</iframe>

<p>An iframe with no borders:</p>
<iframe src="/default.asp" width="100%" height="300" style="border:none;">
</iframe>

</body>
</html>

It ran without anyproblems
Posted
Updated 10-Feb-22 2:11am

1 solution

That Google Sites page has a content security policy[^] applied. Unless your page is hosted on either sites.google.com or google-admin.corp.google.com, it cannot include the page from Google Sites in an <iframe>.
Content-Security-Policy: base-uri 'self';object-src 'none';report-uri /_/view/cspreport;script-src 'nonce-kTJrTU3cDGn9YFmKcreUcQ' 'unsafe-inline' 'unsafe-eval';worker-src 'self';frame-ancestors https://google-admin.corp.google.com/

You're also going to hit the same-origin policy: unless your page is hosted on sites.google.com, it won't be able to access or manipulate the DOM of a page loaded from sites.google.com.

If you're in control of both pages, you may be able to use postMessage()[^] to overcome that restriction.
 
Share this answer
 
Comments
sebastian m 2022 12-Feb-22 13:46pm    
Thank you so much for the tip. I chose another host for my webpage and my code with the iframe tag worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900