window.open () Access is denied-access denied situation and analysis _ basics in script

Source: Internet
Author: User
Tags generator wrapper xmlns
Maybe this situation some friends already know, but I still put their own analysis of ideas written down to other friends for reference.

Yesterday put the blog page another existence, in the local debugging and change blog skin.

Brief introduction:
I want to use script control when the onload page is loaded, add run HTML code, Copycode, Savecode these three buttons dynamically to the CSS class named Code_textarea in a multiline text box.

Status Description:
The push button on the non server side (that is, not debugging on the local server) has all appeared under the multiline text box, but when debugging the script (click Run HTML Code), a denial of access occurs ... Debugging is normal under the server.

Analysis:
Looking at the code, it turns out that the script here is wrong and the code is as follows:
function Fnrunhtmlcode (obj)
{
try {
if (typeof (obj) = = "string") Obj=document.getelementbyid (obj);
var wnd = window.open ("," _blank ",");
Wnd.document.open (' text/html ', ' replace ');
Wnd.document.writeln (Obj.value);
Wnd.document.close ();
catch (ex) {alert (ex.message)};
};
Open with Mozilla, run regular, and there are no errors in the script. It took half an hour, I deleted the rest of the code, in Mozilla, dynamically generated div and button code copy out to see, the dynamic load on IE button copy into the DW look, no exception. This involves putting the function on another page, still normal, and changing the function to window.open (', ', ', '), or deny access.

Since it is denied access, the general must be cross-domain, but local debugging, no additional code ah, how to cross the domain? Puzzled by the solution.
So I want to put on the blog to say it first. The strange thing is to put the server on it, so I put the local page to run under the local server, is also normal. This time is even more strange. Say IE bug? Not tenable because the server can run. Code problems? Not valid because it is normal on other pages and does not involve other code. Just funtion, the note must be downloaded this page has a problem, do csdn blog output HTML page has document.domain similar code?

Step-by-Step Debugging success:
Remove all the original code that is not needed (including HTML). The final simplification is as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" > <!--saved from Url= (0053) http://blog.csdn.net/BlueDe stiny/category/184440.aspx--> <ptml 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 "> <script type=" text/javascript "> Function addevent (o,w,f) {if (document.all) {O.attach Event (' On ' +w, F); else {O.addeventlistener (W, F, false); } function Fnrunhtmlcode (obj) {var wnd = window.open (', ', ', '); }; function Fnreadytorunhtmlcode () {insertrunhtmlbtn (document.getElementById ("demo")); function insertrunhtmlbtn (obj) {var wrapper = document.createelement ("DIV"); Wrapper.style.margin= "10px 0 10px 0"; var runbtn = document.createelement ("INPUT"); Runbtn.type = "button"; Runbtn.value = "Run HTML Code"; Addevent (RUNBTN, "click", Function () {Fnrunhtmlcode (obj)}); Obj.parentNode.insertBefore (wrapper, obj.nextsibling); Wrapper.appendchild (RUNBTN); //} catch (ex) {} onload = Fnreadytorunhtmlcode; </script> <div class= "PostText" > <textarea class= "Code_textarea" id= "demo" > </textarea> &L T;/div> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


Run unchanged.
And then the DTD is removed.

<ptml xmlns= "http://www.w3.org/1999/xhtml" ><pead><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 cont Ent= "text/html; Charset=utf-8 "> <script type=" text/javascript "> Function addevent (o,w,f) {if (document.all) {O.attach Event (' On ' +w, F); else {O.addeventlistener (W, F, false); } function Fnrunhtmlcode (obj) {var wnd = window.open (', ' _blank ', '); Wnd.document.open (' text/html ', ' replace '); Wnd.document.writeln (Obj.value); Wnd.document.close (); }; function Fnreadytorunhtmlcode () {insertrunhtmlbtn (document.getElementById ("demo")); function insertrunhtmlbtn (obj) {var wrapper = document.createelement ("DIV"); Wrapper.style.margin= "10px 0 10px 0"; var runbtn = document.createelement ("INPUT"); Runbtn.type = "button"; Runbtn.value = "Run HTML Code"; Addevent (RUNBTN, "click",function () {Fnrunhtmlcode (obj)}); Obj.parentNode.insertBefore (wrapper, obj.nextsibling); Wrapper.appendchild (RUNBTN); //} catch (ex) {} onload = Fnreadytorunhtmlcode; </script> <div class= "PostText" > <textarea class= "Code_textarea" id= "Demo" > </textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


Normal operation ~ ~ Know the reason.
<!--saved from Url= (0053) http://blog.csdn.net/BlueDestiny/category/184440.aspx-->
This sentence I find for a long time reason, finally planed it out, prove this sentence save as the code in IE will have cross-domain existence, and Mozilla to this sentence no response.

Postscript:
For note codes that are saved as appear
<!--saved from url=....-->
In any part of the HTML, there is a denial of access error in the local server-side debugging, and Mozilla does not. Such as:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<body>
<!--saved from Url= (0053) http://blog.csdn.net/BlueDestiny/category/184440.aspx-->
<input type= "button" value= "Run" onclick= "window.open" (",", ")"/>
</body>

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.