Window. open () access is denied-access denied in the script

Source: Internet
Author: User

Some friends may already know this situation, but I still write down my own analysis ideas for reference by other friends.

Yesterday, another blog page exists. debug and modify the blog skin locally.

Introduction:
I want to use script control when loading the onload page, and dynamically Add the Run HTML code, CopyCode, and SaveCode to the multi-line text box named code_textarea In the css class.

Status description:
On the non-server side (that is, debugging is not performed on the local server), the buttons are all displayed in the multi-line text box, but when debugging the script (click Run HTML code ), access denied... debugging on the server is normal.

Analysis:
Check the code and find that the script has an error. The Code is as follows:
Function fnRunHtmlCode (obj)
{
// Try {
// If (typeof (obj) = "string") obj = document. getElementById (obj );
Var wnd = window. open (''," _ blank ",'');
Wnd.doc ument. open ('text/html ', 'replace ');
Wnd.doc ument. writeln (obj. value );
Wnd.doc ument. close ();
//} Catch (ex) {alert (ex. message )};
};
Open with Mozilla, run regular expressions, and the script has no errors. I deleted all the other code for half an hour. in Mozilla, I copied the dynamically generated div and button code to view the code, there is no exception in copying the Dynamically Loaded buttons on IE to dw. This includes placing the above function on another page for running, and changing it to window. open ('','', '') or deny access.

Since access is denied, generally it must be cross-origin, but local debugging does not add other code. How can this problem be solved? Think twice about it.
So I want to add my blog first. The strange thing is that you can put it on the server again, so I put the local page under the local server to run, it is also normal. This is even more embarrassing. Is it an IE bug? Not true because the server can run. Code problems? Not true, because other pages are normal and no other code is involved. It's just funtion. It means there must be a problem with the downloaded page. Does the HTML page output by CSDN blog have code similar to document. domain?

Step-by-Step debugging successful:
Remove all unnecessary parts of the original code (including Html ). The final simplification is as follows:

<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <HEAD> <TITLE> never-online's Tech Blog in CSDN-JS & DHTML </TITLE> <META content = "MSHTML 6.00.2800.1400" name = GENERATOR> <META http-equiv = Content-Type content = "text/html; charset = UTF-8 "> <pre class =" code "class =" code_textarea "id =" demo "> </textarea> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

The operation remains the same.
Then, the DTD is removed.

<HTML xmlns = "http://www.w3.org/1999/xhtml"> <HEAD> <TITLE> never-online's Tech Blog In CSDN-JS & DHTML </TITLE> <META content = "MSHTML 6.00.2800.1400" name = GENERATOR> <META http-equiv = Content-Type content = "text/html; charset = UTF-8 "> <pre class =" code "class =" code_textarea "id =" demo "> </textarea>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

Running properly ~~~ The cause is unknown.
<! -- Saved from url = (0053) http://blog.csdn.net/BlueDestiny/category/184440.aspx -->
This sentence caused me to find the cause for a long time. I finally figured it out and proved that the code saved as this sentence exists across domains in IE, while Mozilla did not respond to this sentence.

Postscript:
For the comment code that appears when saving
<! -- Saved from url =... -->
If it is placed in any part of HTML, access is denied during local non-server-side debugging, but Mozilla does not. For example:
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head> <Body>
<! -- Saved from url = (0053) http://blog.csdn.net/BlueDestiny/category/184440.aspx -->
<Input type = "button" value = "run" onclick = "window. open ('', '','') "/>
</Body>
</Html>

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.