Hi All
Hope you can help - really stuck with this one…
Goal is to be able to redirect to another url based upon an intersection within a cross-domain iframe.
Desired process is as such…
- A user is on example.com/apps/ - and fills in a form which is within a cross-domain iframe.
- Upon submitting this form within the iframe a
parent.postMessage()
art the end of theload
event which posts a JSON string back to the current page. - Based in the information relied back the user is then redirected from the ‘parent’ site to a new url (also within parent site - eg. example.com/apps/foobar)
I have got as far as step 2 insofar as I am managing to get receipt of the JSON object but am struggling to then get the page to redirect as desired.
I think the issue may also be in listening for when the JSON object becomes available (see screenshot)
Any pointers will be gratefully received - am not at home with the level of JS I’m afraid
jQuery(document).ready(function($) {
window.addEventListener('message', function(event) {
var meta = JSON.stringify(event.data)
var data = JSON.parse(meta);
console.log(data);
// this is the json object received once the form is submitted in the iframe { "applicantid": “12345”, "sector": “foobar” }
// and this is the url I want the user to be automatically redirected to when the parent website received the json object
// https://exampe.com/apps/sector/?applicantid=12345
var applicantid = data.applicantid; // 12345
var sector = data.sector; // foobar
if(applicantid !== undefined) {
var url = 'https://exampe.com/apps/' + sector + '/?cand_id=' + applicantid + '/';
$(location).attr('href',url);
// or maybe this: window.location = 'https://jubileetalent.com/refs/' + sector + '/?cand_id=' + applicantid + '/';
}
});
});
(I am aware there a probably better ways to to do this - we are restricted to this (or a similar method) due to the organisational structure of the client)
Thanks in advance