How to get a request from the parent page?

I am using iframe ipage on my parent page. I would like to receive a request in the javascript of the parent page?

+4
source share
2 answers

I suggest you use my favorite function:

function getQueryString() { var queryStringKeyValue = window.parent.location.search.replace('?', '').split('&'); var qsJsonObject = {}; if (queryStringKeyValue != '') { for (i = 0; i < queryStringKeyValue.length; i++) { qsJsonObject[queryStringKeyValue[i].split('=')[0]] = queryStringKeyValue[i].split('=')[1]; } } return qsJsonObject; } 

Just call it from a child window like this, and act using the query string as an object.

For example, if you have a query string ?name=stack , and you want to get it, try:

 getQueryString().name 

This will return stack .

+13
source

good answer from @Marawan. - if this helps someone ... I expanded this to select the target as a parameter (self / parent)

 function getQueryString(target) { if ( target == 'parent' ) { var queryStringKeyValue = window.parent.location.search.replace('?', '').split('&'); } else { var queryStringKeyValue = window.location.search.replace('?', '').split('&'); } var qsJsonObject = {}; if (queryStringKeyValue != '') { for (i = 0; i < queryStringKeyValue.length; i++) { qsJsonObject[queryStringKeyValue[i].split('=')[0]] = queryStringKeyValue[i].split('=')[1]; } } return qsJsonObject; } 

eg.

 getQueryString('parent').id; // get iframe parent url ?id=foo getQueryString().id; // get this url ?id=foo 
0
source

Source: https://habr.com/ru/post/1379073/


All Articles