When the js parent page calls the child page data, the Child page calls back the specific method of the parent page through the parameter passed over the parent page

Source: Internet
Author: User

When the js parent page calls the child page data, the Child page calls back the specific method of the parent page through the parameter passed over the parent page

When I wrote the code today, I found that the same subpage needs to be called in multiple places on the same page. If multiple methods are called, the method of the parent page callback on the same subpage will fail, so I checked the information, to make this function generic, callback the specific parent PAGE method based on the specific method. By the way, I hope it will be useful in the future and may help people who need help.


Used hereEval() Function

Definition and usage

The eval () function computes a string and executes the JavaScript code.



The parent page calls the sub-page path (the Sub-page path) as follows:

Http: // localhoust: 8080/oss-portlet/html/util/area.html? Oid = 4 & name = Test & callBack = callBackDistrict

Callback:The parameter is the specific method name of the parent page.


The child page calls the method specified by callback of the parent page


Eval ('window. opener. '+ getParams ("callBack") +' (\ ''+ ids + '\', \'' + names + '\')');

Window. close ();


/**
* Get the page path parameter value
*/
Function getParams (key ){
Var href = too many Doc ument. location. href;
If (href. indexOf ("? ")! =-1 ){
Var paramStr = href. substring (href. indexOf ("? ") + 1, href. length );
If (paramStr. indexOf (key + "= ")! =-1 ){
ParamStr = paramStr. substring (paramStr. indexOf (key + "= "),
ParamStr. length );
If (paramStr. indexOf ("&")! =-1 ){
Return paramStr. substring (paramStr. indexOf ("=") + 1, paramStr
. IndexOf ("&"));
} Else {
Return paramStr. substring (paramStr. indexOf ("=") + 1,
ParamStr. length );
}
} Else {
Return "";
}
}
}

Related Article

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.