The JS array of the parent window's child window

Source: Internet
Author: User

Now this system, with a large number of selectors and automatic completion, with my three-inch not rotten hand, these selectors and automatic completion of the most simplified, bon voyage.

This afternoon to add a page selector, to take a complex JSON image, to fill the data.
This JSON object contains an array, taken out, to be transformed into an instance of a "class" of a local page (the parent page of the selector).

I use this method to convert a JSON object to an object of a "class" :

1_.automap =function(data, type, Writenotexistsproperty, maps, callback, propertywrap) {2 ///<summary>3 ///Convert a Json object to an object of the target type4 ///</summary>5 ///<param name= "Data" > </param>6 ///<param name= "type" > Target type </param>7 ///<param name= "Writenotexistsproperty" > if the corresponding property does not exist in the target type, write, default does not write </param>8 ///<param Name= "Maps" > attribute map list, such as {propertya: ' PA ', propertyb: ' B ',...} </param>9 ///<returns type= "" > Target type of data, if the original data is an array, the result is an array of target types </returns>Ten if(Data = =NULL) One return NULL; A  - if(DatainstanceofArray) { - varItem, result = []; the  for(vari = 0; item = Data[i]; i++) { - Result.push (Automap (item, type, Writenotexistsproperty, maps, callback, propertywrap)); - } - returnresult; +}Else if(DatainstanceofObject) { - varresult =Newtype (); +  for(varKinchdata) { A varTargetProperty =K; at varValue =Data[k]; -  - if(Maps! =NULL) { - if(typeof(Maps[k]) = = "string") { - if(typeof(propertywrap) = = "function") -TargetProperty =Propertywrap (maps[k]) in Else -TargetProperty =Maps[k]; to}Else if(typeof(Maps[k]) = = "function") { +Value =Automap (value, Maps[k], Mswriteprofilermark, callback, Propertywrap) - } the}Else if(typeof(propertywrap) = = "function") { *Value =Propertywrap (value); $ }Panax Notoginseng  - //not strictly equal to the if(Result[targetproperty]!== undefined | |Writenotexistsproperty) { +Result[targetproperty] =value; A } the } + if(CallbackinstanceofFunction) { - callback (result, data); $ } $ returnresult; - } -}

Results after the shipment, found that there is no actual implementation, tracking, found that the problem appears in the data instanceof Array this judgment.
Passed in, "I think" is clearly the number of groups, the result is an object, it is depressed.

Perhaps you JS master disagree, here I write a simple example, restore the scene, to find the problem.
There is a A/b two pages, B simulates my selector, that is, a child window.

A.html

1 <HTML>2 <Body>3 <iframesrc= "b.html"ID= "B"></iframe>4 <Script>5 vara= [1,2,3]6 Alert ("a.html" + typeof(a)+ " " +(AinstanceofArray))7 8 functionalertsub (o) {9 Alert ("alertsub without window" + typeof(o)+ " " +(oinstanceofArray)) ;Ten } One  A functionAlertsubwithwindow (w,o) { - Alert ("alertsub with Window" + typeof(o)+ " " +(oinstanceofW.array)) ; - } the  - functionAlertsubvartype (o) { - Alert (typeof(o)); - } + </Script> - </Body> + </HTML>


B.html

1 <HTML>2 <Body>3 <Script>4 vara= NewArray (1,2,3);5 varb= "AAA";6 varC= NewDate ();7 varD=  -;8 vare= false;9 Window.parent.alertSub (a);Ten Window.parent.alertSubWithWindow (window,a); One Window.parent.alertSubVarType (b); A Window.parent.alertSubVarType (c); - Window.parent.alertSubVarType (d); - Window.parent.alertSubVarType (e); the </Script> - </Body> - </HTML>

B.html does nothing but declares several variables, calling the method in the parent window (a.html).

There is a var a = new Array (b.html), by Window.parent.alertSub (a) in the parent page with the xxx instanceof array to determine if it is an array
Guess what the result is? The new array is declared!

What is the popup? is False!
The egg hurts for a while.

Thought for a moment and found a mistake that was ignored.
Array/date these built-in objects are underneath the Window object.
We declare that Array is not generally written as a new window. Array, because the scope of the current page is window.
It's a common sense, but it's easy to be overlooked.

To do a test:


Window.parent.alertSubWithWindow (Window,a);
...
function Alertsubwithwindow (w,o) {
Alert ("Alertsub with Window" + typeof (O) + "" + (o instanceof w.array));
}


This time it's true.

Change the selector, this is the original writing:
var data = $.parsejson ($ (this). attr ("Data-port-info"));
Now change to:
var data = Window.parent.$.parsejson ($ (this). attr ("Data-port-info"));

The JSON is parsed using the $.parsejson of the parent page and then passed to the parent page.

-----------------------------------------------------------------------------------------------------

cnm! Gsq

A long time ago, there is a personal matter to me that the promotion assessment is the comprehensive ability

Let me take a look at this comprehensive ability:

I:
Ability 70 + Flattery 10 Total 80

He (generally, please do not take the seat, if the check can also):
Ability 40 + Flattery + 50 Total 90

10 points more than me.

JS array of child window from parent window

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.