Cross-page Operation error

Source: Internet
Author: User

" Current period error " Uncaught domexception:blocked a frame with origin "null" from accessing a cross-origin frame at HtmlAnchor Element.writecontent ...

you want to load different content into the IFRAME by clicking on different links in the page . Put all kinds of code first (code from "Basic JavaScript Tutorial Nineth Edition" [US · Dori Smith,tom Negrino]):

CSS (SCRIPT01.CSS):

{    background-color: #FFF;}  {    float: right;     Border: 1px solid black;     width: 350px;     height: 300px;     margin-top: 100px;}

HTML (index.html):

<!DOCTYPE HTML><HTML><Head>    <title>IFRAME 4</title>    <Scriptsrc= "Script05.js"></Script>     <Linkrel= "stylesheet"href= "Script01.css"></Head><Body>    <iframesrc= "iframe01.html"ID= "Icontent"name= "Icontent"></iframe>    <H1>Main Content Area</H1>    <H2>    <ahref="#">Link 1</a><BR>    <ahref="#">Link 2</a><BR>    <ahref="#">Link 3</a>    </H2> </Body></HTML>

IFRAME original page (iframe01.html):

<!DOCTYPE HTML><HTML><Head>    <title>Content iframe</title></Head><Body>Please load a page</Body></HTML>

JavaScript (script05.js):

Window.onload =initlinks;varPageCount =NewArray (0,0,0,0);functioninitlinks () { for(vari=0; i<document.links.length; i++) {Document.links[i].onclick=writecontent; Document.links[i].thispage= I+1; }}    functionwritecontent () {pagecount[ This. thispage]++; varNewText = " This. Thispage; NewText+ = ". <br>you has been to this page"; NewText+ = pagecount[ This. Thispage] + "times.<\/h1>"; document.getElementById ("Icontent"). ContentWindow.document.body.innerHTML =NewText; return false;}

In the browser open index.html, regardless of how to click on the link, the content in the IFRAME is always the same. Open the console to see why, as shown below:

It's the kind of mistake that comes up at the beginning.

Cross-page operations involve the concept of " domain ". Uncaught domexception:blocked A frame with origin ' null ' from accessing a Cross-origin frame at Htmlanc  Horelement.writecontent ... Here, origin is the meaning of "domain", and Cross-origin is "cross-domain". Block a frame page with a domain "null" to access a cross-domain Frame page ...

Cross-domain problem refers to the current domain name JS can only read the window properties under the same domain. Causes of cross-domain problems: The URL of a website consists of a protocol name, subdomain, primary domain name, port number. such as https://github.com/, where HTTPS is the protocol name, www is a subdomain, GitHub is the primary domain name, the port number is 80, when in the page from a URL request data, if the URL of the protocol name, subdomain, primary domain, port number any one has a different , a cross-domain problem occurs (from the home of the script).

There are several ways to solve cross-domain problems, and the workaround for the cross-domain problem I have encountered is simple, just put all the code on the server and execute it. After solving the problem, you can successfully click on each link to load different content with IFRAME.

Cross-page Operation error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.